首页 > 解决方案 > 如何在屏幕上获取当前使用的应用程序名称?

问题描述

我想检测当前打开的应用程序并根据 Android 应用程序执行一些操作。我的应用程序是否在后台运行。如果您想对我投反对票,我很感激,但请在评论中提及您为什么这样做?

我可以在下面的 Lollipop 版本中得到它,但不是任何适用于 Lollipop 以上的工作源。

我正在使用GET_TASKS权限,但不推荐使用上述 KitKat Android 版本。

<uses-permission android:name="android.permission.GET_TASKS" /> 

如何在 android 中获取当前打开的应用程序?

    public class ApplicationReceiver extends BroadcastReceiver {

        public final String TAG = "QnA_ApplicationReceiver";
        int co = 0;
        String[] messagnerPack;


    @Override
    public void onReceive(Context aContext, Intent anIntent) {

        messagnerPack = aContext.getResources().getStringArray(R.array.smsAppPackage);
        try {
            ActivityManager am = (ActivityManager) aContext.getSystemService(ACTIVITY_SERVICE);

            List<ActivityManager.RunningTaskInfo> alltasks = am.getRunningTasks(1);
            Log.i("current task :", "CURRENT Activity ::" + alltasks.get(0).topActivity.getClassName());

            for (ActivityManager.RunningTaskInfo aTask : alltasks) {

                // Used to check for CALL screen
                if (aTask.topActivity.getClassName().equals("com.android.phone.InCallScreen") || aTask.topActivity.getClassName().equals("com.truecaller.ui.TruecallerInit") || aTask.topActivity.getClassName().equals("com.android.contacts.DialtactsActivity") || aTask.topActivity.getClassName().contains("com.android.dialer")) {

                    // When user on call screen show a alert message
                    Toast.makeText(aContext, "Phone Call Screen.", Toast.LENGTH_LONG).show();
                }

                // Used to check for SMS screen
                if (isSMSApp(aTask.topActivity.getClassName()) || isSMSApp(aTask.baseActivity.getClassName())) {

                    Toast.makeText(aContext, "SMS App is Active.", Toast.LENGTH_LONG).show();
                }
                // Used to check for CURRENT example main screen
                String packageName = aContext.getPackageName();

                if (aTask.topActivity.getClassName().equals(packageName + ".MainActivity")) {
                    // When opens this example screen then show a alert message

                    //Toast.makeText(aContext, "Running QnA.vbagetech.com Application.", Toast.LENGTH_LONG).show();
                }


                Log.i(TAG, "_________________________________");
                Log.i(TAG, "aTask.baseActivity: " + aTask.baseActivity.flattenToShortString());
                Log.i(TAG, "aTask.baseActivity: " + aTask.baseActivity.getClassName());
                Log.i(TAG, "aTask.topActivity: " + aTask.topActivity.flattenToShortString());
                Log.i(TAG, "aTask.topActivity: " + aTask.topActivity.getClassName());
                Log.i(TAG, "_________________________________");
            }

        } catch (Throwable t) {            
            Log.i(TAG, "Throwable caught: " + t.getMessage(), t);
        }
    }

    boolean isSMSApp(String pack) {
        boolean found = false;
        for (int i = 0; i < messagnerPack.length; i++) {
            if (pack.indexOf(messagnerPack[i]) != -1 ? true : false) {
                found = true;
                break;
            }
        }
        return found;
    }
}

来自 String.XML 的应用程序数组

<string-array name="smsAppPackage">
        <item>com.google.android.apps.messaging</item>
        <item>com.android.mms</item>
        <item>com.whatsapp</item>
        <item>com.jb.gosms</item>
</string-array>

通过搜索,人们说你必须使用

<uses-permission
        android:name="android.permission.REAL_GET_TASKS"
        android:protectionLevel="signature" />

但它给了我错误权限只授予以上版本的系统应用程序。

请帮助我如何解决上述版本的问题。

我的主要摩托是为用户禁用短信应用程序。

标签: android

解决方案


推荐阅读