android 5.0 获取 当前app包名

android 5.0 , 已经不能按此方式获取包名:
ComponentName localComponentName = ((ActivityManager.RunningTaskInfo) ((ActivityManager) getSystemService("activity")).getRunningTasks(1).get(0)).topActivity;
String nowPkgName = localComponentName.getPackageName();

经过查找资料,发现有新的方法获取:

public static String getCurrentPkgName(Context context) {
        RunningAppProcessInfo currentInfo = null;
        Field field = null;
        int START_TASK_TO_FRONT = 2;
        String pkgName = null;
        try {
            field = RunningAppProcessInfo.class.getDeclaredField("processState");
        } catch (Exception e) { e.printStackTrace(); }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();
        for (RunningAppProcessInfo app : appList) {
            if (app.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                Integer state = null;
                try {
                    state = field.getInt( app );
                } catch (Exception e) { e.printStackTrace(); }
                if (state != null && state == START_TASK_TO_FRONT) {
                    currentInfo = app;
                    break;
                }
            }
        }
        if (currentInfo != null) {
            pkgName = currentInfo.processName;
        }
        return pkgName;
}


获取的是进程名, 一般就是 app 包名,如果是自定义进程的,则要另外区分.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 5.0 及以上的版本中,由于安全性增强,无法直接通过包名启动或置顶应用程序。需要您先获取应用程序的启动 `Intent`,然后使用该 `Intent` 启动或置顶应用程序。 以下是示例代码,用于根据包名启动或置顶应用程序: ```java public void openOrTopApp(Context context, String packageName) { PackageManager pm = context.getPackageManager(); Intent launchIntent = pm.getLaunchIntentForPackage(packageName); if (launchIntent != null) { // 应用程序已经安装,启动或置顶应用程序 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(Integer.MAX_VALUE); boolean isAppRunning = false; for (ActivityManager.RunningTaskInfo task : taskList) { if (task.topActivity.getPackageName().equals(packageName)) { isAppRunning = true; break; } } if (isAppRunning) { // 应用程序已经在前台运行,将其置顶 launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); } context.startActivity(launchIntent); } else { // 应用程序未安装 Toast.makeText(context, "应用程序未安装", Toast.LENGTH_SHORT).show(); } } ``` 在这个示例中,我们首先使用包管理器 `PackageManager` 获取应用程序的启动 `Intent`,如果应用程序已经安装,我们就使用该 `Intent` 启动或置顶应用程序。 如果应用程序已经在前台运行,我们就将 `Intent` 添加 `FLAG_ACTIVITY_REORDER_TO_FRONT` 标志来将其置顶;否则,我们将 `Intent` 添加 `FLAG_ACTIVITY_NEW_TASK` 标志来启动应用程序。 请注意,由于 Android 5.0 及以上版本的安全性增强,如果您想要启动或置顶其他应用程序,您需要在应用程序清单文件中声明相应的权限。例如,如果您想要启动或置顶系统应用程序,您需要在应用程序清单文件中声明 `android.permission.INTERACT_ACROSS_USERS_FULL` 权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值