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—— ...
随机推荐
- Javascript做模糊查询
<html> <head> <title>Javascript模糊查找</title> </head> <body> <l ...
- Unity3D手游-横版ACT游戏完整源代码下载
说明: 这不是武林.这不是江湖,没有道不完的恩怨,没有斩不断的情仇,更没有理不清的烦恼,这是剑的世界,一代剑魁闯入未知世界,将会为这个世界展开什么样的蓝图.让你来创造它的未来,剑魁道天下,一剑斗烛龙! ...
- winform基础——实现简易赈灾物资发放登记系统
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 一个简单的webdynpro的ALV示例
开发alv的时候需要1.在web dynpro组件下面 的已使用的组件中添加ALV组件 SALV_WD_TABLE 2.在组件控制器的属性下面创建ALV组件SALV_WD_TABLE 3.在视图界面的 ...
- 【转】有效修改max open files/ulimit -n
[转]有效修改max open files/ulimit -n_追梦20121222_新浪博客 [转]有效修改max open files/ulimit -n (2011-11-18 0 ...
- Linux系统基础命令
这是看itercast的学习笔记 Linux系统基础命令 日期时间 命令date用以查看.设置当前系统时间:格式化显示时间: +%Y--%m--%d 命令hwclock(clock)用以显示硬件时钟时 ...
- Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37737213 上篇博客已经实现了地图的定位以及结合了方向传感器用户路痴定位方向, ...
- Swift - 给项目导入资源
如果想添加资源到项目中去,只要通过鼠标左键将文件或者存有文件的文件夹直接拖到Xcode中. 当松开鼠标后会弹出如下面板: (1)勾上“Copy items if needed”就会拷贝文件进项目, ...
- 手动配置S2SH三大框架报错(二)
十二月 08, 2013 9:34:39 下午 org.apache.catalina.core.AprLifecycleListener init 严重: An incompatible versi ...
- javascript(五)验证
<input id="domo" type="text"> <script> function my_function(){ var ...