Android 调用其他应用-QQ音乐

启动其他应用通用代码:

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName cn = new ComponentName("packageName", "className");
        intent.setComponent(cn);
        startActivity(intent);

想要知道一个apk的包名和启动类名,cmd调用sdk/build-tools下的aapt.exe,输入aapt dump badging xxx.apk

关注package:name='com.tencent.qqmusic'

launchable-activity:name='com.tencent.qqmusic.activity.AppStarterActivity'

这个就是包名和他的启动类名。

更多获取方式:http://blog.csdn.net/a7779486/article/details/52277554


只知道包名,不知道启动类的获取方法

private void doStartApplicationWithPackageName(String packagename) {  
  
    // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等  
    PackageInfo packageinfo = null;  
    try {  
        packageinfo = getPackageManager().getPackageInfo(packagename, 0);  
    } catch (NameNotFoundException e) {  
        e.printStackTrace();  
    }  
    if (packageinfo == null) {  
        return;  
    }  
  
    // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent  
    Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);  
    resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
    resolveIntent.setPackage(packageinfo.packageName);  
  
    // 通过getPackageManager()的queryIntentActivities方法遍历  
    List resolveinfoList = getPackageManager()  
            .queryIntentActivities(resolveIntent, 0);  
  
    ResolveInfo resolveinfo = resolveinfoList.iterator().next();  
    if (resolveinfo != null) {  
        // packagename = 参数packname  
        String packageName = resolveinfo.activityInfo.packageName;  
        // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]  
        String className = resolveinfo.activityInfo.name;  
        // LAUNCHER Intent  
        Intent intent = new Intent(Intent.ACTION_MAIN);  
        intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  
        // 设置ComponentName参数1:packagename参数2:MainActivity路径  
        ComponentName cn = new ComponentName(packageName, className);  
  
        intent.setComponent(cn);  
        startActivity(intent);  
    }  
}  

你可能感兴趣的:(Android)