Android
在一个程序中进行某个操作时,打开另一个程序。
已知包名和启动界面的全类名
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("org.venus.mpep", "org.venus.mpep.ui.activity.LoginActivity");
intent.setComponent(cn);
startActivity(intent);
其中"org.venus.mpep"是需要打开的应用程序的包名, "org.venus.mpep.ui.activity.LoginActivity"是需要打开应用程序的启动界面全类名
仅知道包名
try {
pi = getActivity().getPackageManager().getPackageInfo("org.venus.mprs", 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
} catch (PackageManager.NameNotFoundException e) {
ToastUtil.show(getActivity(),"打开应用程序出错");
e.printStackTrace();
}
其中"org.venus.mprs"是需打开的应用程序的包名
其实可以看出来2个方式是一样的操作,区别是一个指定启动界面,一个需要通过包名查询到启动界面再指定
因此可以把这个功能写成一个工具类,以后用的时候直接调用就行
package org.venus.library.utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.List;
/**
* Author Jxx _让世界看到我
* On 2021/12/3
* Note TODO
*/
public class PackagesUtil {
/**
* 打开一个app
*
* @param context
* @param packageName
* @param launcherActivityClass
*/
public static void openApp(Context context, String packageName, String launcherActivityClass) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, launcherActivityClass);
intent.setComponent(cn);
context.startActivity(intent);
}
/**
* 打开一个app
*
* @param context
* @param packageName
*/
public static void openApp(Context context, String packageName) throws Exception {
PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0);
PackageManager pm = context.getPackageManager();
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
String thisPackageName = ri.activityInfo.packageName;
String thisClassName = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(thisPackageName, thisClassName);
intent.setComponent(cn);
context.startActivity(intent);
} else {
throw new NullPointerException(packageName + "-没有找到对应的App");
}
}
}
网友评论