18
18
*/
19
19
public class IntentLauncherModule extends ReactContextBaseJavaModule implements ActivityEventListener {
20
20
private static final String ATTR_ACTION = "action" ;
21
+ private static final String ATTR_TYPE = "type" ;
21
22
private static final String ATTR_CATEGORY = "category" ;
22
23
private static final String TAG_EXTRA = "extra" ;
23
24
private static final String ATTR_DATA = "data" ;
24
25
private static final String ATTR_FLAGS = "flags" ;
26
+ private static final String ATTR_PACKAGE_NAME = "packageName" ;
27
+ private static final String ATTR_CLASS_NAME = "className" ;
25
28
Promise promise ;
26
29
27
30
public IntentLauncherModule (ReactApplicationContext reactContext ) {
@@ -40,33 +43,29 @@ public String getName() {
40
43
* getReactApplicationContext().startActivity(intent);
41
44
*/
42
45
@ ReactMethod
43
- public void startActivity (ReadableMap params , final Promise promise ){
46
+ public void startActivity (ReadableMap params , final Promise promise ) {
44
47
this .promise = promise ;
45
48
try {
46
- ComponentName cn = new ComponentName (params .getString (ATTR_DATA ), params .getString (ATTR_ACTION ));
47
- Intent intent = new Intent ();
48
- intent .setComponent (cn );
49
- Log .d ("IntentLauncherModule" , "Opening" );
50
- if (params .hasKey (TAG_EXTRA )) {
51
- intent .putExtras (Arguments .toBundle (params .getMap (TAG_EXTRA )));
52
- }
53
- if (params .hasKey (ATTR_FLAGS )) {
54
- intent .addFlags (params .getInt (ATTR_FLAGS ));
55
- }
56
- if (params .hasKey (ATTR_CATEGORY )) {
57
- intent .addCategory (params .getString (ATTR_CATEGORY ));
58
- }
59
- getReactApplicationContext ().startActivityForResult (intent , 12 , null ); // 暂时使用当前应用的任务栈
60
- } catch (Exception e ) {
61
- promise .reject ("ERROR" , "Could not open intent" );
62
- }
63
- }
64
-
65
- @ ReactMethod
66
- public void startActivityOnlyIntent (ReadableMap params , final Promise promise ) {
67
- this .promise = promise ;
68
- try {
69
- Intent intent = new Intent (params .getString (ATTR_DATA ));
49
+ Intent intent = new Intent ();
50
+
51
+ if (params .hasKey (ATTR_CLASS_NAME )) {
52
+ ComponentName cn ;
53
+ if (params .hasKey (ATTR_PACKAGE_NAME )) {
54
+ cn = new ComponentName (params .getString (ATTR_PACKAGE_NAME ), params .getString (ATTR_CLASS_NAME ));
55
+ } else {
56
+ cn = new ComponentName (getReactApplicationContext (), params .getString (ATTR_CLASS_NAME ));
57
+ }
58
+ intent .setComponent (cn );
59
+ }
60
+ if (params .hasKey (ATTR_ACTION )) {
61
+ intent .setAction (params .getString (ATTR_ACTION ));
62
+ }
63
+ if (params .hasKey (ATTR_DATA )) {
64
+ intent .setData (Uri .parse (params .getString (ATTR_DATA )));
65
+ }
66
+ if (params .hasKey (ATTR_TYPE )) {
67
+ intent .setType (params .getString (ATTR_TYPE ));
68
+ }
70
69
if (params .hasKey (TAG_EXTRA )) {
71
70
intent .putExtras (Arguments .toBundle (params .getMap (TAG_EXTRA )));
72
71
}
@@ -83,44 +82,45 @@ public void startActivityOnlyIntent (ReadableMap params, final Promise promise)
83
82
}
84
83
85
84
@ Override
86
- public void onNewIntent (Intent intent ) { }
85
+ public void onNewIntent (Intent intent ) {
86
+ }
87
87
88
88
@ Override
89
89
public void onActivityResult (Activity activity , int requestCode , int resultCode , Intent data ) {
90
- if (requestCode != 12 ) {
91
- return ;
92
- }
93
- WritableMap params = Arguments .createMap ();
94
- Bundle extras = data .getExtras ();
95
-
96
- Set <String > keys = extras .keySet ();
97
- Iterator <String > it = keys .iterator ();
98
- Log .e ("LGC" ,"Dumping Intent start" );
99
- while (it .hasNext ()) {
100
- String key = it .next ();
101
-
102
- if (extras .get (key ) instanceof String ) {
103
- params .putString (key , (String ) extras .get (key ));
104
- }
90
+ if (requestCode != 12 ) {
91
+ return ;
92
+ }
93
+ WritableMap params = Arguments .createMap ();
94
+ if (data != null ) {
95
+ Bundle extras = data .getExtras ();
105
96
106
- if (extras .get (key ) instanceof Integer ) {
107
- params .putInt (key , (Integer ) extras .get (key ));
108
- }
97
+ Set <String > keys = extras .keySet ();
98
+ Iterator <String > it = keys .iterator ();
99
+ Log .e ("LGC" , "Dumping Intent start" );
100
+ while (it .hasNext ()) {
101
+ String key = it .next ();
109
102
110
- if (extras .get (key ) instanceof Double ) {
111
- params .putDouble (key , (Double ) extras .get (key ));
112
- }
103
+ if (extras .get (key ) instanceof String ) {
104
+ params .putString (key , (String ) extras .get (key ));
105
+ }
106
+
107
+ if (extras .get (key ) instanceof Integer ) {
108
+ params .putInt (key , (Integer ) extras .get (key ));
109
+ }
110
+
111
+ if (extras .get (key ) instanceof Double ) {
112
+ params .putDouble (key , (Double ) extras .get (key ));
113
+ }
113
114
114
- if (extras .get (key ) instanceof Boolean ) {
115
- params .putBoolean (key , (Boolean ) extras .get (key ));
115
+ if (extras .get (key ) instanceof Boolean ) {
116
+ params .putBoolean (key , (Boolean ) extras .get (key ));
117
+ }
118
+
119
+ Log .e ("LGC" , "[" + key + "=" + extras .get (key ) + "]" );
116
120
}
117
-
118
- Log .e ("LGC" , "[" + key + "=" + extras .get (key ) + "]" );
119
121
}
120
122
121
- // params = Arguments.fromBundle(extras);
122
-
123
- this .promise .resolve (params );
123
+ this .promise .resolve (params );
124
124
125
125
}
126
126
}
0 commit comments