@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v("BACKGROUND", "程序进入后台");
showNotification();
}
// 点击HOME键时程序进入后台运行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
// 按下HOME键
if(keyCode == KeyEvent.KEYCODE_HOME){
// 显示Notification
notification = new NotificationExtend(this);
notification.showNotification();
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
package com.test.background;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
/**
* Notification扩展类
* @Description: Notification扩展类
* @File: NotificationExtend.java
* @Package com.test.background
* @Author Hanyonglu
* @Date 2012-4-13 下午02:00:44
* @Version V1.0
*/
public class NotificationExtend {
private Activity context;
public NotificationExtend(Activity context) {
// TODO Auto-generated constructor stub
this.context = context;
}
// 显示Notification
public void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (
NotificationManager)context.getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification = new Notification(
R.drawable.icon,"阅读器",
System.currentTimeMillis());
// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知自动清除。
notification.flags |= Notification.FLAG_AUTO_CANCEL
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
// 设置通知的事件消息
CharSequence contentTitle = "阅读器显示信息"; // 通知栏标题
CharSequence contentText = "推送信息显示,请查看……"; // 通知栏内容
Intent notificationIntent = new Intent(context,context.getClass());
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
context, contentTitle, contentText, contentIntent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
}
// 取消通知
public void cancelNotification(){
NotificationManager notificationManager = (
NotificationManager) context.getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
}
<activity android:name="ShowMessageActivity"
android:launchMode="singleTask"></activity>
import java.util.List;
import com.test.background.MainActivity;
import com.test.background.NotificationExtend;
import com.test.background.R;
import com.test.util.AppManager;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.util.Log;
/**
* 监听程序是否在前后台运行Service
* @Description: 监听程序是否在前后台运行Service
* @FileName: AppStatusService.java
* @Package com.test.service
* @Author Hanyonglu
* @Date 2012-4-13 下午04:13:47
* @Version V1.0
*/
public class AppStatusService extends Service{
private static final String TAG = "AppStatusService";
private ActivityManager activityManager;
private String packageName;
private boolean isStop = false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
packageName = this.getPackageName();
System.out.println("启动服务");
new Thread() {
public void run() {
try {
while (!isStop) {
Thread.sleep(1000);
if (isAppOnForeground()) {
Log.v(TAG, "前台运行");
} else {
Log.v(TAG, "后台运行");
showNotification();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
/**
* 程序是否在前台运行
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the device
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;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("终止服务");
isStop = true;
}
// 显示Notification
public void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (
NotificationManager)getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification = new Notification(
R.drawable.icon,"阅读器",
System.currentTimeMillis());
// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 点击后自动清除Notification
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
// 设置通知的事件消息
CharSequence contentTitle = "阅读器显示信息"; // 通知栏标题
CharSequence contentText = "推送信息显示,请查看……"; // 通知栏内容
Intent notificationIntent = new Intent(AppManager.context,AppManager.context.getClass());
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
AppManager.context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
AppManager.context, contentTitle, contentText, contentIntent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
}
}
/**
*判断当前应用程序处于前台还是后台
*
* @param context
* @return
*/
public static boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
<uses-permission android:name="android.permission.GET_TASKS" />
/**
*
* @param context
* @return
*/
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
Log.i(String.format("Background App:", appProcess.processName));
return true;
}else{
Log.i(String.format("Foreground App:", appProcess.processName));
return false;
}
}
}
return false;
}
@Override
protected void onUserLeaveHint() { //当用户按Home键等操作使程序进入后台时即开始计时
super.onUserLeaveHint();
if(!isLeave){
isLeave=true;
saveStartTime();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有