Android判断应用程序从后台回到前台
MainActivity如下:
package cc.testbackgroundtofront; import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;
/**
* Demo描述:
* 判断应用从后台回到前台
* 即按下Home键后重新进入到应用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} @Override
protected void onResume() {
super.onResume(); } @Override
protected void onRestart() {
super.onRestart();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isCurrentRunningForeground = prefs.getBoolean("isCurrentRunningForeground", false);
if (!isCurrentRunningForeground) {
Toast.makeText(MainActivity.this, "从后台回来", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
try {
Thread.sleep(1000);
new Thread(){
public void run() {
boolean isCurrentRunningForeground=isRunningForeground();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor=prefs.edit();
editor.putBoolean("isCurrentRunningForeground", isCurrentRunningForeground);
editor.commit();
};
}.start();
} catch (Exception e) { }
} public boolean isRunningForeground(){
String packageName=getPackageName(this);
String topActivityClassName=getTopActivityName(this);
System.out.println("packageName="+packageName+",topActivityClassName="+topActivityClassName);
if (packageName!=null&&topActivityClassName!=null&&topActivityClassName.startsWith(packageName)) {
System.out.println("---> isRunningForeGround");
return true;
} else {
System.out.println("---> isRunningBackGround");
return false;
}
} public String getTopActivityName(Context context){
String topActivityClassName=null;
ActivityManager activityManager =
(ActivityManager)(context.getSystemService(android.content.Context.ACTIVITY_SERVICE )) ;
//android.app.ActivityManager.getRunningTasks(int maxNum)
//int maxNum--->The maximum number of entries to return in the list
//即最多取得的运行中的任务信息(RunningTaskInfo)数量
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1) ;
if(runningTaskInfos != null){
ComponentName f=runningTaskInfos.get(0).topActivity;
topActivityClassName=f.getClassName(); }
//按下Home键盘后 topActivityClassName=com.android.launcher2.Launcher
return topActivityClassName;
} public String getPackageName(Context context){
String packageName = context.getPackageName();
return packageName;
} }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="判断应用是否是由后台回到前台"
android:layout_centerInParent="true"
/> </RelativeLayout>
Android判断应用程序从后台回到前台的更多相关文章
- iOS 学习 - 24 全局跑马灯,支持后台回到前台
思路: 1.创建一个单例 + (instancetype)shareManager { static CCPaomaView *pModel = nil; static dispatch_once_t ...
- android 判断应用程序是否已安装
1.判断是否安装/** check the app is installed*/private boolean isAppInstalled(Context context,String packag ...
- iOS App 获取从后台返回前台时的页面
产品美美的给小伙伴提了一个需求,当程序从后台进入前台时,如果是指定的页面,则弹出提示框. 大家首先想到的方法就是通过 AppDelegate.h 进行控制,相对复杂的步骤就是 在程序进入后台时对当前页 ...
- iOS 在视图控制器里面判断 应用程序的前台 后台切换 UIViewController
1.时机 用户点击home 键 应用退到后台 再次点击进入前台 在UIViewController里面 控制器如何获取相关的事件? 2.需求 (1)NSTimer 在应用程序进入后台 10秒 ...
- Android 监听APP进入后台或切换到前台方案对比
在我们开发的过程中,经常会遇到需要我们判断app进入后台,或者切换到前台的情况.比如我们想判断app切换到前台时,显示一个解锁界面,要求用户输入解锁密码才能继续进行操作:我们想判断app切换到后台,记 ...
- C# 后台获取前台交互判断
前台传来明细 ,判断是否修改,在把前台 的数据组成新的类保存 public class tt { public string id { get; set; } public string e_id { ...
- android 判断应用是否在前台显示
在一些场景下我们需要知道应用是否在前台显示,当不在前台显示的时候,一些后台进程可以暂时停止,比如一些查询任务.不必要的线程.不需要的渲染等,以减少对设备资源的占用.判断应用是否在前台通常可以使用一下方 ...
- Android判断App是否在前台运行
版权声明:本文为博主原创文章,未经博主允许不得转载. //当前应用是否处于前台 private boolean isForeground(Context context) { if (context ...
- 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇(二)
在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议内容,在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP—— ...
随机推荐
- uva 10692 - Huge Mods(数论)
题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...
- Servlet的学习之Cookie
从本篇开始学习Servlet技术中的Cookie专题. 首先来了解什么是“会话”.会话是web技术中的一个术语,可以简单的理解为:用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭 ...
- Opencv2系列学习笔记8(图像滤波)
一:概念: 滤波是信号处理机图像处理中的一个基本操作.滤波去除图像中的噪声,提取感兴趣的特征,允许图像重采样. 图像中的频域和空域:空间域指用图像的灰度值来描述一幅图像:而频域指用图像灰度值的变化来描 ...
- metasploit学习之ms03_026
傻瓜式利用ms03_026_dcom: Matching Modules ================ Name Disclosure Date Rank Description ---- --- ...
- mysql基础:mysql列类型--时间和日期
mysql列类型--整型 http://blog.csdn.net/jk110333/article/details/9342283 mysql列类型--字符串http://blog.csdn.net ...
- _00024 尼娜抹微笑伊拉克_云计算ClouderaManager以及CHD5.1.0群集部署安装文档V1.0
笔者博文:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...
- Eclipse在点击运行后不能自动保存的解决
今天在eclipse上写程序调试时,发现当我点击运行按键之后,并不能在运行前帮我自动保存,也就是说每次修改代码之后, 运行的还是前一次运行之前的代码,并不是修改之后的代码,因此通过在网上搜索解决方案之 ...
- ExtJS拖拽效果
ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...
- js传真实地址 C:\fakepath
js给action传真是地址的时候,处于安全,传到action中 浏览器会改变路径变为C:\fakepath\ftp.txt,但是原始路径却是 C:\Documents and Settings\Ad ...
- Git 图解剖析(转)
git中文件内容并没有真正存储在索引(.git/index)或者提交对象中,而是以blob的形式分别存储在数据库中(.git/objects),并用SHA-1值来校验. 索引文件用识别码列出相关的bl ...