Skip to content

Commit e4206db

Browse files
merged 'startActivity' and 'startActivityOnlyIntent' in one method and added few other options: 'type', 'packageName', 'className'.
1 parent 530c286 commit e4206db

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
*/
1919
public class IntentLauncherModule extends ReactContextBaseJavaModule implements ActivityEventListener {
2020
private static final String ATTR_ACTION = "action";
21+
private static final String ATTR_TYPE = "type";
2122
private static final String ATTR_CATEGORY = "category";
2223
private static final String TAG_EXTRA = "extra";
2324
private static final String ATTR_DATA = "data";
2425
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";
2528
Promise promise;
2629

2730
public IntentLauncherModule(ReactApplicationContext reactContext) {
@@ -40,33 +43,29 @@ public String getName() {
4043
* getReactApplicationContext().startActivity(intent);
4144
*/
4245
@ReactMethod
43-
public void startActivity(ReadableMap params, final Promise promise){
46+
public void startActivity(ReadableMap params, final Promise promise) {
4447
this.promise = promise;
4548
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+
}
7069
if (params.hasKey(TAG_EXTRA)) {
7170
intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA)));
7271
}
@@ -83,44 +82,45 @@ public void startActivityOnlyIntent (ReadableMap params, final Promise promise)
8382
}
8483

8584
@Override
86-
public void onNewIntent(Intent intent) { }
85+
public void onNewIntent(Intent intent) {
86+
}
8787

8888
@Override
8989
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();
10596

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();
109102

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+
}
113114

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) + "]");
116120
}
117-
118-
Log.e("LGC", "[" + key + "=" + extras.get(key) + "]");
119121
}
120122

121-
// params = Arguments.fromBundle(extras);
122-
123-
this.promise.resolve(params);
123+
this.promise.resolve(params);
124124

125125
}
126126
}

0 commit comments

Comments
 (0)