活动失去焦点,由于在相同的应用程序的另一个活动VS一个单独的应用程序/设备中断 [英] Activity losing focus due to another Activity in the same app vs a separate app / device interrupt

查看:178
本文介绍了活动失去焦点,由于在相同的应用程序的另一个活动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).

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

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.

这篇关于活动失去焦点,由于在相同的应用程序的另一个活动VS一个单独的应用程序/设备中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆