活动失去焦点,由于在相同的应用程序的另一个活动VS一个单独的应用程序/设备中断应用程序、焦点、设备、VS

由网友(寒塘冷月)分享简介:我要记录一个事件,只有当应用程序作为一个整体已失去焦点(如来电或home键)。I need to log an event only when the application as a whole has lost focus (such as an incoming call or the home key).什...

我要记录一个事件,只有当应用程序作为一个整体已失去焦点(如来电或home键)。

I need to log an event only when the application as a whole has lost focus (such as an incoming call or the home key).

什么是告诉一个活动已暂停或外部事件停止,而不是在相同的应用程序?

What is the best way to tell an Activity has been paused or stopped from an external event rather than another activity within the same app?

感谢

推荐答案

当你的活动停止后,你可以检查你的应用程序仍然在顶部的的onStop()方法

when your activity stops you can check if your app is still on top in the onStop() method:

private boolean isAppOnTop(){
    ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> tasks; 
    tasks = am.getRunningTasks(1); 
    RunningTaskInfo running = tasks.get(0);
    if(running.topActivity.getPackageName().equals("your.package.name")){
        return true;
    }
    else{
        return false;
    }
}

此方法查询所有正在运行的任务第一。一般有重点的任务是在堆栈的顶部。检查这个任务属于你的包。

This method queries all running tasks first. In general the task which has focus is on top of the stack. Check if this task belongs to your package.

阅读全文

相关推荐

最新文章