1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| package com.anloq.nfcservice;
import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.util.Log;
import com.anloq.MyApplication; import com.anloq.activity.AdActivity; import com.anloq.utils.DetectionASUtils;
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Timer; import java.util.TimerTask;
/** * Created by xpf on 2017/6/3 :) * 检测APP页面是否一直运行,不运行就直接启动 */
public class MonitoringService extends Service {
private final static String TAG = "MonitoringService";
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if ("kill_self".equals(intent.getAction())) { Log.e(TAG, "onReceive:杀死自己的进程!"); killMyselfPid(); // 杀死自己的进程 } } };
private Timer timer = new Timer(); private TimerTask task = new TimerTask() { @Override public void run() { checkIsAlive(); } };
/** * 检测应用是否活着 */ private void checkIsAlive() { String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date()); Log.e(TAG, "CustodyService Run: " + format);
boolean AIsRunning = CheckUtil.isClsRunning( MonitoringService.this, "com.xpf.monitor", "com.xpf.monitor.activity.AActivity"); boolean BIsRunning = CheckUtil.isClsRunning( MonitoringService.this, "com.xpf.monitor", "com.xpf.monitor.activity.BActivity"); boolean b = (AIsRunning || BIsRunning); boolean CIsRunning = CheckUtil.isClsRunning( MonitoringService.this, "com.xpf.monitor", "com.xpf.monitor.activity.CActivity");
Log.e(TAG, "AIsRunning || BIsRunning is running:" + b + ",CIsRunning:" + CIsRunning);
if (!CIsRunning) { if (!b) { //如果界面挂掉直接启动AActivity Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(MonitoringService.this, AActivity.class); startActivity(intent); } } }
@Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate: 启动监控服务! "); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("kill_self"); registerReceiver(broadcastReceiver, intentFilter); timer.schedule(task, 0, 10000);// 设置检测的时间周期(毫秒数) }
@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }
@Override public IBinder onBind(Intent arg0) { return null; }
/** * 杀死自身的进程 */ private void killMyselfPid() { int pid = android.os.Process.myPid(); String command = "kill -9 " + pid; Log.e(TAG, "killMyselfPid: " + command); stopService(new Intent(MonitoringService.this, MonitoringService.class)); try { Runtime.getRuntime().exec(command); System.exit(0); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); if (task != null) { task.cancel(); } if (timer != null) { timer.cancel(); } } }
|