注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

Android 监听程序进入后台或恢复到前台  

2014-05-21 13:59:18|  分类: Android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
//轉
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
 
public class BaseActivity extends Activity {
        @Override
        protected void onStop() {
                super.onStop();
                if (!isAppOnForeground()) {
                        //app 进入后台                        
                        //全局变量isActive = false 记录当前已经进入后台
                }
        }
 
        @Override
        protected void onResume() {
                super.onResume();
                //if (!isActive) {
                        //app 从后台唤醒,进入前台                        
                        //isActive = true;
                //}
        }
 
        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<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
                if (appProcesses == null)
                        return false;
                for (RunningAppProcessInfo appProcess : appProcesses) // The name of the process that this object is associated with.
                        if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                                return true;
                return false;
        }
}

public class BaseActivity extends Activity {
    private static final String TAG = "zz";
    private static SharedPreferences pref;
    private static boolean isLeave = false;

    public OfficeBaseActivity() {
        super();
    }   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getSharedPreferences("timeoutcheck", Context.MODE_PRIVATE);
    }

    @Override
    protected void onUserLeaveHint() { //当用户按Home键等操作使程序进入后台时即开始计时
        super.onUserLeaveHint();
        if(!isLeave){
            isLeave=true;
            saveStartTime();
        }
    }
   
    @Override
    protected void onResume() { //当用户使程序恢复为前台显示时执行onResume()方法,在其中判断是否超时.
        super.onResume();
        if(isLeave){
            isLeave=false;
            timeOutCheck();
        }
    }

    public void timeOutCheck() {
        long endtime = System.currentTimeMillis();
        Log.i(TAG, "endtime  == " + endtime);
        Log.i(TAG, "starttime  == " + getStartTime());
        Log.i(TAG, "endtime - getStartTime == " + (endtime - getStartTime()));
        if (endtime - getStartTime()>= 30 * 1000)
            Toast.makeText(this, "超时了", Toast.LENGTH_SHORT).show();
    }

    public void saveStartTime() {
        pref.edit().putLong("starttime", System.currentTimeMillis()).commit();
    }

    public long getStartTime() {
        return pref.getLong("starttime", 0);
    }
}
  评论这张
 
阅读(1658)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017