通过startActivity启动第三方应用的Activity时崩溃Permission Denial(比如打开微信朋友圈)

按键精灵手机版可以通过

RunApp "com.tencent.mm", ".plugin.sns.ui.SnsTimeLineUI"

直接打开微信朋友圈

我尝试用

ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.plugin.sns.ui.SnsTimeLineUI");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivity(intent);

结果获得以下错误

09-15 13:15:53.087  11188-11188/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.lee.myapplication, PID: 11188
    java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.tencent.mm/.plugin.sns.ui.SnsTimeLineUI } from ProcessRecord{42ec77e8 11188:com.example.lee.myapplication/u0a103} (pid=11188, uid=10103) not exported from uid 10092
            at android.os.Parcel.readException(Parcel.java:1472)
            at android.os.Parcel.readException(Parcel.java:1426)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2185)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1425)
            at android.app.Activity.startActivityForResult(Activity.java:3476)
            at android.app.Activity.startActivityForResult(Activity.java:3437)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
            at android.app.Activity.startActivity(Activity.java:3679)
            at android.app.Activity.startActivity(Activity.java:3647)
            at com.example.lee.myapplication.MainActivity$1.onClick(MainActivity.java:70)
            at android.view.View.performClick(View.java:4469)
            at android.view.View$PerformClick.run(View.java:18807)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5315)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
            at dalvik.system.NativeStart.main(Native Method)
阅读 43.7k
2 个回答

只有当Activity向外声明了自己是可以处理某些intent action时,第三方app才能通过intent去启动它。
通过配置文件中的intent过滤器来声明:

<activity android:name=".Activity_A" >
    <intent-filter >
        <action android:name="android.intent.action.VIEW"/>
    </intent-filter>
</activity>

当知道了package name和class name,可以这样去启动第三方的activity:

Intent i = new Intent("android.intent.action.VIEW");
String pkg = "me.li2.test";
String cls = "me.li2.test.Activity_A";
i.setComponent(new ComponentName(pkg, cls));
startActivity(i);

这样做是没问题的。但假如没有在manifest中声明,startActivity将导致崩溃。你可以写两个app做个测试,添加、去掉intent声明。

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW cmp=.SnsTimeLineUI } from ...... not exported from uid 10092

PS: 通过package name可以启动第三方app的launcher activity,当我尝试启动微信时失败,原因不明。

startActivity(getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));

赞。解决了我的问题,直接startActivity(getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));可进入微信。但是为什么

Intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(comp);
startActivity(intent);

会报错呐?

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.tecent.mm/com.tecent.mm.ui.LauncherUI}; have you declared this activity in your AndroidManifest.xml?
推荐问题
宣传栏