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

按键精灵手机版可以通过

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

全部回覆(2)
Ty80

只有當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?
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板