我有两个应用App1
和App2
我想从App2
打开App1
的第三个活动,我想在应用程序之间传递一些值。我怎么能这样做??
我试过这个:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.aap2.MainActivity3");
startActivity(LaunchIntent);
但我得到例外。
01-23 00:18:04.150: E/AndroidRuntime(5323): java.lang.IllegalStateException: Could not execute method of the activity
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.view.View$1.onClick(View.java:2144)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.view.View.performClick(View.java:2485)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.view.View$PerformClick.run(View.java:9080)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.os.Handler.handleCallback(Handler.java:587)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.os.Looper.loop(Looper.java:130)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.app.ActivityThread.main(ActivityThread.java:3714)
01-23 00:18:04.150: E/AndroidRuntime(5323): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 00:18:04.150: E/AndroidRuntime(5323): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 00:18:04.150: E/AndroidRuntime(5323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-23 00:18:04.150: E/AndroidRuntime(5323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-23 00:18:04.150: E/AndroidRuntime(5323): at dalvik.system.NativeStart.main(Native Method)
01-23 00:18:04.150: E/AndroidRuntime(5323): Caused by: java.lang.reflect.InvocationTargetException
01-23 00:18:04.150: E/AndroidRuntime(5323): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 00:18:04.150: E/AndroidRuntime(5323): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.view.View$1.onClick(View.java:2139)
01-23 00:18:04.150: E/AndroidRuntime(5323): ... 11 more
01-23 00:18:04.150: E/AndroidRuntime(5323): Caused by: java.lang.NullPointerException
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.app.Activity.startActivityForResult(Activity.java:2827)
01-23 00:18:04.150: E/AndroidRuntime(5323): at android.app.Activity.startActivity(Activity.java:2933)
01-23 00:18:04.150: E/AndroidRuntime(5323): at com.example.aap1.MainActivity1.one(MainActivity1.java:27)
答案 0 :(得分:3)
问题出在这里
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.aap2.MainActivity3");
在这里你必须传递packagename
,但是你传递的是Activity
名称..以上内容将返回你在清单文件中设置的启动器活动..
为此,您需要像这样更改代码..
Intent i = new Intent();
i.setClassName("com.example.aap2", "com.example.aap2.MainActivity3");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
并在Manifest中为android:export="true"
设置MainActivity3
。
答案 1 :(得分:2)
使用
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app2", "com.example.aap2.MainActivity3"));
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Make sure this falg is really need.
startActivity(intent);
有用的资源
您需要将清单中的活动声明写为(对于第二个应用)
<activity android:name=".MainActivity3"
android:label="@string/app_name"
android:exported="true" >
// Add intent filter if any.
</activity>
答案 2 :(得分:1)
仅在您不知道活动名称时使用此功能,并且不像您那样传递活动名称
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.aap2");
否则只需尝试以下方法,打开另一个活动的常用方法是
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);