似乎有两个问题,我遇到了麻烦。第一种情况是当我启动时钟应用程序并完美运行时,这似乎是我启动大多数应用程序时的情况。
第二种情况是当我尝试启动诸如电话活动之类的活动时。当我去获取Intent时,getLaunchIntentForPackage(app.pname)返回null。是否有不同的方式来获取电话活动的意图,以便我可以在这种情况下启动它?
第三种情况是我去发起一项活动,比如Camera活动。似乎我必须在我的androidManifest.xml文件中安装Camera活动来启动它,但还有另一种方法可以解决这个问题吗?我的应用程序具有从其UI启动任何应用程序的功能,因此我无法事先将Android设备中的每个活动添加到我的清单文件中。谢谢!
爪哇:
Intent launchApp = getPackageManager().getLaunchIntentForPackage(app.pname);
System.out.println(Intent of " + appName + ": + " " +launchApp);
if(launchApp!=null){
System.out.println("Permission granted to launch " + appName);
startActivity( launchApp );
}
else{
System.out.println("Permission not granted to launch " + appName);
}
logcat的:
08-07 09:38:57.031: I/System.out(1056): Intent of Clock: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.htc.android.worldclock/.WorldClockTabControl }
08-07 09:38:57.041: I/System.out(1056): Permission granted to launch Clock
08-07 09:38:59.783: I/System.out(1056): Intent of Phone: null
08-07 09:38:59.783: I/System.out(1056): Permission not granted to launch Phone
08-07 09:39:10.564: I/System.out(1056): Intent of Camera: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.android.camera/com.android.internal.app.ResolverActivity }
08-07 09:39:10.574: I/System.out(1056): Permission granted to launch Camera
08-07 09:39:10.574: D/AndroidRuntime(1056): Shutting down VM
08-07 09:39:10.574: W/dalvikvm(1056): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
08-07 09:39:10.614: E/AndroidRuntime(1056): FATAL EXCEPTION: main
08-07 09:39:10.614: E/AndroidRuntime(1056): java.lang.IllegalStateException: Could not execute method of the activity
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.view.View$1.onClick(View.java:2191)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.view.View.performClick(View.java:2532)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.view.View$PerformClick.run(View.java:9293)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.os.Handler.handleCallback(Handler.java:587)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.os.Looper.loop(Looper.java:150)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.app.ActivityThread.main(ActivityThread.java:4306)
08-07 09:39:10.614: E/AndroidRuntime(1056): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 09:39:10.614: E/AndroidRuntime(1056): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 09:39:10.614: E/AndroidRuntime(1056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-07 09:39:10.614: E/AndroidRuntime(1056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-07 09:39:10.614: E/AndroidRuntime(1056): at dalvik.system.NativeStart.main(Native Method)
08-07 09:39:10.614: E/AndroidRuntime(1056): Caused by: java.lang.reflect.InvocationTargetException
08-07 09:39:10.614: E/AndroidRuntime(1056): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 09:39:10.614: E/AndroidRuntime(1056): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.view.View$1.onClick(View.java:2186)
08-07 09:39:10.614: E/AndroidRuntime(1056): ... 11 more
08-07 09:39:10.614: E/AndroidRuntime(1056): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.camera/com.android.internal.app.ResolverActivity}; have you declared this activity in your AndroidManifest.xml?
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1565)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1539)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.app.Activity.startActivityForResult(Activity.java:2919)
08-07 09:39:10.614: E/AndroidRuntime(1056): at android.app.Activity.startActivity(Activity.java:3025)
08-07 09:39:10.614: E/AndroidRuntime(1056): at com.worklight.androidnativeapp.AndroidNativeApp.appClicked(AndroidNativeApp.java:284)
08-07 09:39:10.614: E/AndroidRuntime(1056): ... 14 more
答案 0 :(得分:1)
启动特定应用,您需要知道包名称和类名
try{
Intent i = new Intent();
i.setClassName("com.example.app", "com.example.app.MainActivity");
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(e);
}catch(ActivityNotFoundException e){
e.printStackTrace();
}