android - 通过startActivity启动第三方应用的Activity时崩溃Permission Denial(比如打开微信朋友圈)
ringa_lee
ringa_lee 2017-04-17 14:37:21
0
2
929

按键精灵手机版可以通过

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)
ringa_lee
ringa_lee

ringa_lee

reply all(2)
Ty80

Only when the Activity declares that it can handle certain intent actions, the third-party app can start it through the intent.
Declare via intent filter in configuration file:

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

When you know the package name and class name, you can start the third-party activity like this:

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

It’s okay to do this. But if it is not declared in the manifest, startActivity will cause a crash. You can write two apps as a test, adding and removing intent statements.

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

PS: The launcher activity of a third-party app can be started through the package name. When I tried to start WeChat, it failed for an unknown reason.

startActivity(getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));
大家讲道理

Like. My problem was solved and I can enter WeChat directly startActivity(getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));. But why

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

Will an error be reported?

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?
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template