Android啓動其他app的Activity

本文介绍了如何在Android中根据已知包名和启动类名启动其他App,以及仅凭包名启动App的方法。同时讲解了启动任意已注册的Activity的条件,强调被启动的Activity需要设置intent-filter,并提到了动态加载已安装APK类的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 已知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<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);
//不知類名調用,需知道已設置的action
Intent 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<ResolveInfo> 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();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值