Intent intent = new Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
ComponentName cn = new ComponentName("com.zhxumao.plugina", "com.zhxumao.plugina.MainActivity")
intent.setComponent(cn)
startActivity(intent)
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageInfo = null
try {
packageInfo = getPackageManager().getPackageInfo("com.zhxumao.plugina",0)
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace()
}
if (packageInfo == null) return
Intent intent = new Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.setPackage(packageInfo.packageName)
//遍歷該Activity的信息
List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(intent,0)
if (resolveInfos != null){
ResolveInfo resolveInfo = resolveInfos.iterator().next()
if (resolveInfo == null){
return
}
ComponentName componentName = new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name)
Intent intentActivity = new Intent(Intent.ACTION_MAIN)
intentActivity.addCategory(Intent.CATEGORY_LAUNCHER)
intentActivity.setComponent(componentName)
startActivity(intentActivity)
}
- 啓動任意已注冊的Activity
被啓動的Activity需要設置intent-filter
<intent-filter>
<action android:name="com.zhxumao.plugina.showActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.zhxumao.plugina", "com.zhxumao.plugina.SecondActivity");
intent.setComponent(cn);
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.zhxumao.plugina.showActivity");
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage("com.zhxumao.plugina");
PackageManager pm = getPackageManager();
List<ResolveInfo> resolveinfoes = pm.queryIntentActivities(intent, 0);
ActivityInfo actInfo = resolveinfoes.get(0).activityInfo;
String apkPath = actInfo.applicationInfo.sourceDir;
String libPath = actInfo.applicationInfo.nativeLibraryDir;
PathClassLoader pcl = new PathClassLoader(apkPath, libPath,
this.getClassLoader());
try {
Class dynamic = pcl.loadClass("com.zhxumao.plugina.SecondActivity");
Method method = dynamic.getDeclaredMethod("ShowToast");
method.invoke(dynamic.newInstance());
} catch (Exception exception) {
exception.printStackTrace();
}