今天在做一个功能,就是在我们自己的app启动另外一个app的某个activity
本来以为这是一件非常容易的事情,毕竟启动另外一个activity发一个intent就可以了,网上一堆代码。类似这样的:
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.example.a13199.testapplication", "com.example.a13199.testapplication.testRecycleView.testJump");
intent.putExtra("action_params"," id=123 name=测试一下");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
这个方法的确是可行的,但是很快你就会发先,这个方法,只能启动另一个app的首页,其他activity全部回报一个错误
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.bingo.sled/com.bingo.sled.activity.HandleLinkShortcutActionActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4030)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:152)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:793)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
at android.support.v4.app.Fragment.startActivity(Fragment.java:1009)
at android.support.v4.app.Fragment.startActivity(Fragment.java:998)
at com.zjd.cbrain.ui.LoginFragment.onViewClicked(LoginFragment.java:217)
at com.zjd.cbrain.ui.LoginFragment_ViewBinding$1.doClick(LoginFragment_ViewBinding.java:32)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5264)
at android.view.View$PerformClick.run(View.java:21297)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
这是因为另外一个app中你要启动的那个activity没有暴露出来
其实要解决非常简单,有两种方法
第一种:
在那个activity的AndroidMainfest文件声明中加上这样一句话
android:exported="true"
如下图这样:
![](https://i-blog.csdnimg.cn/blog_migrate/0ac97e8b6badc073451e5258c23d5365.png)
第二种方法,则是在
那个activity的AndroidMainfest文件声明中加一个action,如下图这样
<activity android:name=".testRecycleView.testJump">
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
然后在启动的那个activity,创建intent的时候,加上那个action来初始化,也就是上面启动方式变成这样
Intent intent = new Intent("android.intent.action.VIEW");
ComponentName cn = new ComponentName("com.example.a13199.testapplication", "com.example.a13199.testapplication.testRecycleView.testJump");
intent.putExtra("action_params"," id=123 name=测试一下");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
然后,搞定啦,这样启动就不会报权限错误了