android - 如何在屏幕上获取当前使用的应用程序名称?
问题描述
我想检测当前打开的应用程序并根据 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" />
但它给了我错误权限只授予以上版本的系统应用程序。
请帮助我如何解决上述版本的问题。
我的主要摩托是为用户禁用短信应用程序。
解决方案
推荐阅读
- reactjs - 我的代码有什么问题使它在 js fiddle 上不起作用?
- django-rest-framework - Django-rest-framework 不会发送用户注册激活邮件
- arrays - 唯一排序如何实现
- c - 函数无明显原因重复运行
- javascript - javascript中此关键字的异常行为
- cakephp - CakePHP:从 3.9.x 升级到 4.0:bin/cake 升级 rector --rules phpunit80 挂起
- linux - linux命令行输入中的硬链接
- javascript - React-window,如何防止状态更改时重新渲染列表?
- flutter - 当我安装任何颤振包时,示例项目还包括?
- javascript - Croppie.js 在浏览图像文件时也会选择视频