【问题标题】:Get the package name of any android app on device when it gets opened by user获取用户打开设备上任何 android 应用程序的包名
【发布时间】:2017-01-25 12:32:51
【问题描述】:

我想在应用打开时获取它的包名。每当任何被 applock 锁定的应用程序打开时,我都想实现类似 applock 应用程序的功能,希望显示锁定屏幕。

我尝试了一些解决方案,但没有成功。 我在服务下面打电话,但没有得到包名

class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Context... params) {
        final Context context = params[0].getApplicationContext();
        return isAppOnForeground(context);
    }

    private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        final String packageName = context.getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {

                Log.e(TAG, "Process Name : " + appProcess.processName);

                return true;

        }
        return false;
    }
}

【问题讨论】:

    标签: android service package


    【解决方案1】:

    可以通过调用this.getClass().getPackage()获取当前类的包。

    该方法的文档在类文档中:https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

    【讨论】:

    • 我想获取用户打开的任何应用的包名。
    • 对不起,我想我误读了你的问题,当你调用“final String packageName = context.getPackageName();”时你得到了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2011-06-03
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多