我正在尝试使用RunningAppProcessInfo在棒棒糖中获得最近应用程序的点击事件,但我没有得到此活动。
for (RunningAppProcessInfo appProcess : appProcesses)
{
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{
if(appProcess.importanceReasonComponent != null)
{
ComponentName comp = appProcess.importanceReasonComponent;
// Log.e("tag", "not null====>>"+comp.getClassName());
if (comp.getClassName().equals("com.android.systemui.recents.RecentsActivity"))
{
Log.e("tag===", "clear when recent app");
ApplicationClass.prefrences.Store_recent_open_app("");
}
}
}
}
在棒棒糖下面,我使用以下代码获得了最近的活动。
List<RunningTaskInfo> tasks = manager.getRunningTasks(1);
for (RunningTaskInfo task : tasks)
{
ComponentName componentName = task.topActivity;
if (componentName.getClassName().equals("com.android.systemui.recent.RecentsActivity"))
{
Log.e("tag", "recent");
ApplicationClass.prefrences.Store_recent_open_app("");
}
}
请帮我解决这个问题。
谢谢。
使用下面的代码:
ActivityManager am = (ActivityManager) getApplicationContext()
.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName;
if(Build.VERSION.SDK_INT>20){
packageName = am.getRunningAppProcesses().get(0).processName;
if (packageName != null && packageName.equals("com.android.systemui.recentsactivity")) {
// TODO do your stuff here
}
} else{
packageName = am.getRunningTasks(2).get(0).topActivity
.getPackageName();
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
// TODO do your stuff here
}
}