异常信息如下
SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.exam/.main.MainActivity } from ProcessRecord{110f6b2 9855:com.example.test/u0a80} (pid=9855, uid=10080) not exported from uid 10079
W/System.err(9855): at android.os.Parcel.readException(Parcel.java:1546)
07-12 17:19:16.247: W/System.err(9855): at android.os.Parcel.readException(Parcel.java:1499)
07-12 17:19:16.247: W/System.err(9855): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2439)
07-12 17:19:16.247: W/System.err(9855): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
07-12 17:19:16.247: W/System.err(9855): at android.app.Activity.startActivityForResult(Activity.java:3745)
07-12 17:19:16.257: W/System.err(9855): at android.app.Activity.startActivityForResult(Activity.java:3706)
07-12 17:19:16.257: W/System.err(9855): at android.app.Activity.startActivity(Activity.java:4016)
07-12 17:19:16.257: W/System.err(9855): at android.app.Activity.startActivity(Activity.java:3984)
07-12 17:19:16.257: W/System.err(9855): at com.example.main.CanvasDemoActivity.openFile6(CanvasDemoActivity.java:157)
07-12 17:19:16.257: W/System.err(9855): at java.lang.reflect.Method.invoke(Native Method)
07-12 17:19:16.257: W/System.err(9855): at java.lang.reflect.Method.invoke(Method.java:372)
代码片如下
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("com.exam", "com.exam.main.MainActivity");
intent.setComponent(cn);
// Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.exam");
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Log.d("wmb", "--open exam App has an exception");
}
在调用第三方应用特定的Activity时,使用指定的包名和类Activity名,如:
ComponentName cn = new ComponentName(“com.exam”, “com.exam.main.MainActivity”);
报异常SecurityException。
根据异常提示可知,from ProcessRecord{110f6b2 9855:com.example.test/u0a80} (pid=9855, uid=10080) not exported from uid 10079
com.exam.main.MainActivity未支持exported属性。
解决方法
在manifest配置文件中,com.exam.main.MainActivity声明处,增加属性android:exported="true"
其他调用第三方App的方式
public void startAPP(String appPackageName){
try{
Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
startActivity(intent);
}catch(Exception e){
Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
}
}