First, you need to add the following permissions to the manifest list
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
public class AppUtil {
//获取到所有的包名并检查是否有指定包名
@SuppressLint("QueryPermissionsNeeded")
public static boolean isAppInstalled(Context context, String packageName){
//应用信息列表
List<ResolveInfo> apps = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//获取到设备上安装的所有应用信息
apps = context.getPackageManager().queryIntentActivities(intent, 0);
for (int i = 0; i < apps.size(); i++) {
ResolveInfo info = apps.get(i);
//包名
String pname = info.activityInfo.packageName;
//主activity信息
CharSequence cls = info.activityInfo.name;
//app名字
CharSequence appname = info.activityInfo.loadLabel(context.getPackageManager());
Log.e("Unity","应用信息: appname = "+appname+" packageName = "+pname+" Activity = "+cls);
if(pname.equals(packageName)){
return true;
}
}
return false;
}
//通过包名跳转到该app
public static void JumpToActivity(Context context, String packageName, String className) {
Intent intent = new Intent();
intent.setClassName(packageName, className);
context.startActivity(intent);
}
}
How to use
public void JumpToApp(String pageName,String activityName){
if(!AppUtil.isAppInstalled(this,pageName)){
TToast.show(this,"应用不存在,请确认已安装该应用");
return;
}
TToast.show(this,"应用存在");
AppUtil.JumpToActivity(UnityPlayerActivity.this, pageName,activityName);
}
The result is as follows:
The jump I wrote before is problematic. When I jump from one app to another, I didn’t create a task stack, so another app will run on the task stack of the current app, which will cause display problems and will jump The conversion code is modified as follows
//通过包名跳转到该app
public static void JumpToActivity(String packageName, String className, Activity curactivity) {
ComponentName componetName = new ComponentName(packageName,className);
Intent intent= new Intent();
intent.setComponent(componetName);
//创建一个任务栈
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
curactivity.startActivity(intent);
}
The complete code is as follows
package com.unity3d.player.tools;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class AppUtil {
//获取到所有的包名并检查是否有指定包名
@SuppressLint("QueryPermissionsNeeded")
public static boolean isAppInstalled(Context context, String packageName){
//应用信息列表
List<ResolveInfo> apps = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//获取到设备上安装的所有应用信息
apps = context.getPackageManager().queryIntentActivities(intent, 0);
for (int i = 0; i < apps.size(); i++) {
ResolveInfo info = apps.get(i);
//包名
String pname = info.activityInfo.packageName;
//主activity信息
CharSequence cls = info.activityInfo.name;
//app名字
CharSequence appname = info.activityInfo.loadLabel(context.getPackageManager());
Log.e("Unity","应用信息: appname = "+appname+" packageName = "+pname+" Activity = "+cls);
if(pname.equals(packageName)){
return true;
}
}
return false;
}
//通过包名跳转到该app
public static void JumpToActivity(String packageName, String className, Activity curactivity) {
ComponentName componetName = new ComponentName(packageName,className);
Intent intent= new Intent();
intent.setComponent(componetName);
//创建一个任务栈
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
curactivity.startActivity(intent);
}
}