按键精灵手机版可以通过
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)
只有當Activity向外聲明了自己是可以處理某些intent action時,第三方app才能透過intent去啟動它。
透過設定檔中的intent過濾器來宣告:
當知道了package name和class name,可以這樣去啟動第三方的activity:
這樣做是沒問題的。但假如沒有在manifest中聲明,startActivity將導致崩潰。你可以寫兩個app做個測試,加入、去掉intent宣告。
讚。解決了我的問題,直接
startActivity(getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));
可進入微信。但為什麼會報錯吶?