Android啓動其他app的Activity
16lz
2022-06-13
- 已知App包名及啓動類名
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
// 通过包名获取此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 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);//不知類名調用,需知道已設置的actionIntent intent = new Intent(); intent.setAction("com.zhxumao.plugina.showActivity"); startActivity(intent);
- 動態加載已安裝apk的類
//動態加載已安裝apk的類 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setPackage("com.zhxumao.plugina"); // 获得包管理器 PackageManager pm = getPackageManager(); List resolveinfoes = pm.queryIntentActivities(intent, 0); // 获得指定的activity的信息 ActivityInfo actInfo = resolveinfoes.get(0).activityInfo; // 获得apk的目录或者jar的目录 String apkPath = actInfo.applicationInfo.sourceDir; // native代码的目录 String libPath = actInfo.applicationInfo.nativeLibraryDir; // 创建类加载器,把dex加载到虚拟机中 // 第一个参数:是指定apk安装的路径,这个路径要注意只能是通过actInfo.applicationInfo.sourceDir来获取 // 第二个参数:是C/C++依赖的本地库文件目录,可以为null // 第三个参数:是上一级的类加载器 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(); }
更多相关文章
- Android(安卓)蓝牙模块框架分析
- Android之存储
- Android(安卓)如何加载大图片
- Android(安卓)Ant批量打包
- Android(安卓)java.lang.IllegalArgumentException: You cannot
- Android(安卓)GPS获得经纬度并得到该坐标精确地址
- 图片加载库Glide的使用
- android ListView监听滚动条滚动到底部时继续加载
- Android(安卓)webView与js 交互以及jsbridge框架源码分析