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—— ...
随机推荐
- 快速排序原理、复杂度分析及C语言实现
本文作者华科小涛:@http://www.cnblogs.com/hust-ghtao/,参考<算法导论>,代码借用<剑指offer> 快速排序是一种最坏情况时间复杂度为的排序 ...
- Eclipse 乱码问题 修改设置
1.工作空间的字符编码设置 2.对于任意的项目,可以右键选择属性,然后修改 3.对于任意一个文件,同样的操作,右键选择属性,然后配置字符编码
- Windows 8 动手实验系列教程 实验7:磁贴和通知
动手实验 实验7:磁贴和通知 2012年9月 简介 磁贴是Windows应用商店应用用户体验的重要元素.当应用程序被安装后,它的磁贴将在Windows 8开始屏幕被创建.该磁贴(称为主磁贴)作为启动应 ...
- VC 实现视图区背景颜色渐变填充
void CSTest1View::OnDraw(CDC* pDC) { CSTest1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: ...
- 【Node.js 自己封装的库 http_parse, libuv】
[Node.js 自己封装的库 http_parse, libuv] Node.js 介绍:一个网络框架,更多:http://www.oschina.net/p/nodejs 官网:http://no ...
- SQL视图和多表连接
本篇博客关注的焦点是视图的使用以及视图和多表连接的配合.以便可以了解视图,以及更好的使用视图. 首先,还是要说明一下视图的定义:视图是基于SQL语句的结果集的可视化虚拟表,换句话说视图就是SQL查询结 ...
- Cocos2dx-Android 之Makefile通用高级写法
不废话,直接贴.节约时间.须要改进的地方请各位指出.共同学习. LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := c ...
- Delphi主窗口任务栏菜单的问题(转发WM_SYSCOMMAND到Application)
Delphi的VCL框架在创建应用时TApplication是一个自动创建的隐藏窗口,其它创建的窗口是自动以该窗口为窗口,这就导致创始的主窗口在任务栏的系统菜单只有三项,只要在主窗口的Create事件 ...
- js错误 SyntaxError: missing : after property id
在用jquery的post方法时 $.post('adminCheckTpmisPlans.do',{'test',str},function(f){ ... }) 报如下错误 SyntaxError ...
- Android Studio IDE Out of Memory
场景: 尝试过各种方式,IDE重装,重新启动,设置IDE MEMORY大小JDK MEMORY大小都无效 终于在FILE->INVALIDATE CACHES/RESTART 中点击重新启动之后 ...