如何将Activity从Main Application Actitvity(名称为MainActivity)移至 子应用程序活动(名称是MainActivity)
public void listAllActivities()抛出NameNotFoundException,ClassNotFoundException {
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(PackageInfo pack : packages)
{
if (pack.packageName.equals("com.ABC.example")){
ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities;
Log.i("PC", pack.packageName + " ::: ");
if(activityInfo!=null)
{
for(int i=0; i<activityInfo.length; i++)
{
Log.i("actlist", pack.packageName + " ::: " + activityInfo[i].name);
if(activityInfo[i].name.equals("com.ABC.example.MainActivity")){
Log.i("activity", pack.packageName + " ::: " + activityInfo[i].name);
Intent i1=new Intent(MainActivity.this,activityInfo[i].name.getClass());
startActivity(i1);
}
}
}
}
}
}
log cat:
11-08 12:00:19.792:E / AndroidRuntime(1184):致命异常:主
11-08 12:00:19.792:E / AndroidRuntime(1184):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.pride / com.example.pride.MainActivity}:java.lang。 SecurityException:Permission Denial:从ProcessRecord启动Intent {cmp = com.ABC.example / .MainActivity} {40684d90
1184:com.example.pride / 10034}(pid = 1184,uid = 10034)需要null
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.os.Handler.dispatchMessage(Handler.java:99)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.os.Looper.loop(Looper.java:123)
11-08 12:00:19.792:E / AndroidRuntime(1184):在android.app.ActivityThread.main(ActivityThread.java:3683)
11-08 12:00:19.792:E / AndroidRuntime(1184):at java.lang.reflect.Method.invokeNative(Native Method)
答案 0 :(得分:0)
您可以从应用中启动另一个应用的Activity
,只要该应用是公开的("exported="true"
或<intent-filter>
)。你这样做:
Intent i1=new Intent(MainActivity.this,activityInfo[i].name.getClass());
startActivity(i1);
这不会起作用,因为您使用了2参数Intent
构造函数。这个构造函数采用&#34;包名称&#34;来自第一个参数(这将包含您的应用程序的包名)和第二个参数中活动的类名。
你想这样做:
Intent i1=new Intent();
i1.setClassName(activityInfo[i].packageName, activityInfo[i].name);
startActivity(i1);
这样,您将使用ActivityInfo
对象中的信息设置包名称和活动类名称。