android开发分享判断Android程序是否在前台运行的两种方法

@override protected void onstop() { if (!isapponforeground()) { debug.

  @override   protected void onstop() {     if (!isapponforeground()) {       debug.i("dwy", "enter background");       misbackground = true;     } else {       debug.i("dwy", "foreground");       misbackground = false;     } 

上述就是android开发分享判断Android程序是否在前台运行的两种方法的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

judge is app in background when onstop() get called.

  public boolean isapponforeground() {       // returns a list of application processes that are running on the       // device          activitymanager activitymanager = (activitymanager) getapplicationcontext().getsystemservice(context.activity_service);       string packagename = getapplicationcontext().getpackagename();          list<activitymanager.runningappprocessinfo> appprocesses = activitymanager           .getrunningappprocesses();       if (appprocesses == null)         return false;          for (activitymanager.runningappprocessinfo appprocess : appprocesses) {         // the name of the process that this object is associated with.         if (appprocess.processname.equals(packagename)             && appprocess.importance == activitymanager.runningappprocessinfo.importance_foreground) {           return true;         }       }       return false;     }   

方法二:

  /**     * 需要权限:android.permission.get_tasks     *     * @param context     * @return     */     public boolean isapplicationbroughttobackground(context context) {       activitymanager am = (activitymanager) context               .getsystemservice(context.activity_service);       list<runningtaskinfo> tasks = am.getrunningtasks(1);       if (tasks != null && !tasks.isempty()) {         componentname topactivity = tasks.get(0).topactivity;         debug.i(tag, "topactivity:" + topactivity.flattentostring());         debug.f(tag, "topactivity:" + topactivity.flattentostring());         if (!topactivity.getpackagename().equals(context.getpackagename())) {           return true;         }       }       return false;     }   

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/941180.html

(0)
上一篇 2021年11月14日
下一篇 2021年11月14日

精彩推荐