我有一个应用程序,我有一个特定的事件接收器。我的目标是在应用程序运行时显示一个对话框,否则(即如果应用程序没有运行)显示通知。
我已实施的通知或显示对话框等事件。我无法确定用户是否启用了该应用程序。
有什么想法吗?
答案 0 :(得分:1)
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1);
ComponentName componentName = runningTaskInfoList.get(0).topActivity;
String runningActivityName = componentName.getClassName();
String runningActivityPackageName = componentName.getPackageName();
需要"android.permission.GET_TASKS"
权限。
答案 1 :(得分:1)
在这里确切 -
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
found = true;
break;
}
}
if(found) {
//Close your app or service
}
}
答案 2 :(得分:1)
public boolean isAppRunning()
{
boolean appFound = false;
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo recentTask : recentTasks)
{
if (recentTask.baseActivity.getPackageName().equals("package.Name"))
{
appFound = true;
break;
}
}
return appFound;
}
还需要许可
<uses-permission android:name="android.permission.GET_TASKS" />
请检查我的问题是否与您的问题有关。请检查评论。