android:检测设置(com.android.settings)是否打开 [英] android: detect if settings(com.android.settings) is open
问题描述
我想当用户尝试打开 Settings
应用或包 com.android.settings
(Settings
中的所有应用,如电池、wifi、声音等),它会显示Toast
.我将在 Service
中创建.这是我的代码:
i want to when user try to open Settings
app or package com.android.settings
(all app in Settings
like battery, wifi, sound, etc), it will show up Toast
. I will create in Service
. Here's my code :
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
//if(Settings is open){
//Toast.makeText . . .
}
return START_STICKY;
}
我已经在 stackoverflow
中完成了所有操作,但仍然无法正常工作.我为我的英语道歉,如果您提出解决方案,我会很高兴.谢谢你..
I have already all in stackoverflow
, but still doesn't work. I apologize for my english, and will be glad if you suggest a solution. Thank You. .
推荐答案
这将获取所有当前正在运行的任务,并循环它们的包名,直到找到(或找不到)您指定的包:
This will get all the currently running tasks, and loop their package names until your specified package is found (or not):
ActivityManager activityManager = (ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager
.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo task : tasks) {
if (task.baseActivity.getPackageName().equalsIgnoreCase("com.android.settings")) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
}
}
还要将此权限添加到您的清单中:
Also add this permission to your manifest:
<uses-permission android:name="android.permission.GET_TASKS" />
这篇关于android:检测设置(com.android.settings)是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!