您的位置:首页 > 移动开发 > Android开发

Android开发之旅一监听程序的前后台运行状态

2014-03-26 11:07 387 查看
package com.mobile.utilitys;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.os.Message;

import com.consumer.asmart.service.FloatWinService;
import com.mobile.activity.MainActivity;
import com.mobile.consumer.R;

/**
* 监听程序是否在前后台运行Service
* 监听程序是否在前后台运行Service
*
* joke
*
*/
public class AppStatusService extends Service{
private static final String TAG = "AppStatusService";
private ActivityManager activityManager;
private String packageName;
private boolean isStop = false;
private   NotificationManager notificationManager;

@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()){
//                        	System.out.println("-----前台运行--------");

if(null != notificationManager){
notificationManager.cancel(1);
}

if(FloatWinService.handler != null ){
Message msg=FloatWinService.handler.obtainMessage();
msg.what=MsgUtil.MSG_QIPAO;
FloatWinService.handler.sendMessage(msg);
}

} else {

if(FloatWinService.handler != null ){
Message msg=FloatWinService.handler.obtainMessage();
msg.what=MsgUtil.MSG_NOQIPAO;
FloatWinService.handler.sendMessage(msg);
}
//                        	System.out.println("-----后台运行--------");
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("终止服务");
notificationManager.cancel(1);
isStop = true;
}

// 显示Notification
public void showNotification() {
// 创建一个NotificationManager的引用
notificationManager = (
NotificationManager)getSystemService(
android.content.Context.NOTIFICATION_SERVICE);

// 定义Notification的各种属性
Notification notification = new Notification(
R.drawable.ic_launcher,"蜂众网",
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 = "test"; // 通知栏标题
CharSequence contentText = "test"; // 通知栏内容

Intent notificationIntent = new Intent(AppStatusService.this,MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
AppStatusService.this, 1, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
AppStatusService.this, contentTitle, contentText, contentIntent);
// 把Notification传递给NotificationManager
notificationManager.notify(1, notification);
}
}
记得在manifest加上
 <!-- 程序前台后台运行服务 --><service android:name="com.mobile.utilitys.AppStatusService" ></service>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: