android基础知识---不同app的跳转,及进程的控制

xiaoxiao2021-02-28  103

这里我简单介绍下跳转的三个方式,直接上方法了

1、已知包名直接按包名跳转

Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.admin.tiaoapp2"); if (intent){ startActivity(intent); }

2、已知包名和activity和包名跳转(Intent.setComponent())

Intent intent = new Intent(); ComponentName componentName=new ComponentName("com.example.admin.tiaoapp2","com.example.admin.tiaoapp2.MainActivity"); intent.setComponent(componentName); startActivity(intent);

这里你可以看到我没做非空判断 下面是网上流传的非常普遍的用法:

String package_name = "xx.xx.xx"; String activity_path = "xx.xx.xx.ab.xxActivity"; Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//可选 ComponentName cn = new ComponentName(package_name,activity_path); intent.setComponent(cn); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { //找不到指定的 Activity }

但是经验证,Intent.resolveActivity() 方法并不能判定此方式所要启动的 Activity 是否存在,如果此 Activity 不存在,会报 Java.lang.IllegalArgumentException: Unknown component 异常,并导致程序崩溃。 所以我选择用resolveActivityInfo,这两种方法非常相似,咱们对比下源码

public ComponentName resolveActivity(PackageManager pm) { if (mComponent != null) { return mComponent; } ResolveInfo info = pm.resolveActivity(this, PackageManager.MATCH_DEFAULT_ONLY); if (info != null) { return new ComponentName( info.activityInfo.applicationInfo.packageName, info.activityInfo.name); } return null; } public ActivityInfo resolveActivityInfo(PackageManager pm, int flags) { ActivityInfo ai = null; if (mComponent != null) { try { ai = pm.getActivityInfo(mComponent, flags); } catch (PackageManager.NameNotFoundException e) { // ignore } } else { ResolveInfo info = pm.resolveActivity(this, PackageManager.MATCH_DEFAULT_ONLY | flags); if (info != null) { ai = info.activityInfo; } } return ai; }

显而易见,resolveActivityInfo最后还是调用了resolveActivity,但是其内部做了处理和判断不会报IllegalArgumentException

所以最后的使用方式

Intent intent = new Intent(); ComponentName componentName=new ComponentName("com.example.admin.tiaoapp2","com.example.admin.tiaoapp2.MainActivity"); intent.setComponent(componentName); if (intent.resolveActivityInfo(getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null) { startActivity(intent); }

3、已知包名跳转遍历找到activity跳转

private void doStartApplicationWithPackageName(String packagename) { // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等 PackageInfo packageinfo = null; try { packageinfo = getPackageManager().getPackageInfo(packagename, 0); } catch (PackageManager.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<ResolveInfo> 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); Log.e("测试 :","packageName:"+packageName+" className:"+className); // 设置ComponentName参数1:packagename参数2:MainActivity路径 ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); startActivity(intent); }

这里小tips下进程:

如果按包名跳转会有两个进程,但是如果按activity跳转则 只有一个进程。(我就是掉坑了—。——)

4隐式启动第三方应用

这里在说下隐式启动第三方应用 此方式多用于启动系统中的功能性应用,比如打电话、发邮件、预览图片、使用默认浏览器打开一个网页等。

Intent intent = new Intent(); intent.setAction(action); intent.addCategory(category); intent.setDataAndType("abc://www.dfg.com","image/gif"); startActivity(intent); 条件1:IntentFilter 至少有一个action 至少有一个Category 可以没有DataType 条件2:如果有Data,参数中Data必须符合Data规则 条件3:Action和Category必须同时匹配Activity中的一个Action和一个Category(Category 默认:android.intent.category.DEFAULT)
转载请注明原文地址: https://www.6miu.com/read-47938.html

最新回复(0)