package com.qht.b;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MonitorService extends Service {
public static final String CLASS_NAME = "MonitorService";
private Thread thread;
private DatagramSocket socket = null;
private Context m_context;
private long lastTimeMillis = 0;
Timer timer = null;
TimerTask task;
boolean m_btimerisStart;
public MonitorService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.d(CLASS_NAME, "onBind !!");
return null;
}
@Override
public void onCreate() {
super.onCreate();
m_context = this;
Log.d(CLASS_NAME, "onCreate !!");
lastTimeMillis = 0;
thread=new Thread(new Runnable()
{
@Override
public void run()
{
try {
System.out.println("监听端口16667");
socket = new DatagramSocket(16667);
socket.setSoTimeout(5000);
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
byte data[] = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
} catch (SocketTimeoutException e) {
System.out.println("socket 10s 超时:" + e.getMessage());
} catch (SocketException e) {
System.out.println("socket SocketException:" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println("socket IOException:" + e.getMessage());
e.printStackTrace();
}
String result = new String(packet.getData(), packet.getOffset(), packet.getLength());
//校验包
if (result.equals("hertbeat"))
{
lastTimeMillis = System.currentTimeMillis();
System.out.println("rec : hertbeat");
}else if (result.equals("login"))
{
if (m_btimerisStart)
{
System.out.println("重复login!!!");
continue;
}
//login
lastTimeMillis = System.currentTimeMillis();
startTimer();
System.out.println("rec : login");
} else if (result.equals("logout"))
{
//退出取消,等待login再开启
System.out.println("rec : logout timer.cancel()");
stopTimer();
} else if (result.equals("anr"))
{
//退出取消,等待login再开启
System.out.println("rec : anr restartApp");
restartApp();
}
}
}
});
thread.start();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(CLASS_NAME, "onStart !!");
}
private void startTimer(){
m_btimerisStart = true;
if (timer == null) {
timer = new Timer();
}
if (task == null) {
task = new TimerTask() {
@Override
public void run() {
System.out.println("run TimerTask");
if (lastTimeMillis != 0 && System.currentTimeMillis()- lastTimeMillis > 2000)
{
System.out.println("失去心跳,拉起APPlastTimeMillis :" + lastTimeMillis + ":" + (Calendar.getInstance().getTimeInMillis() - lastTimeMillis) );
// 心跳超时,杀死并拉起
restartApp();
}
}
};
}
if(timer != null && task != null )
timer.schedule(task,0,1000);
}
private void restartApp() {
SuUtil.kill(ConstantUtil.PACKAGE_NAME);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
PackageManager localObject = m_context.getPackageManager();
if (PackageUtil.checkPackInfo(m_context, ConstantUtil.PACKAGE_NAME)) {
Log.i(CLASS_NAME, "find package, ready to lanunch! "+ConstantUtil.PACKAGE_NAME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
Log.i(CLASS_NAME, "Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE "+ (int)Build.VERSION.SDK_INT);
// m_context.startActivity((localObject).getLaunchIntentForPackage(ConstantUtil.PACKAGE_NAME));
Intent action = new Intent(Intent.ACTION_VIEW);
action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
StringBuilder builder = new StringBuilder();
builder.append("nova://aodingplayer");
action.setData(Uri.parse(builder.toString()));
startActivity(action);
}
} else {
Log.i(CLASS_NAME, "not find package!" + ConstantUtil.PACKAGE_NAME);
}
lastTimeMillis = 0;
stopTimer();
}
private void stopTimer(){
m_btimerisStart = false;
if (timer != null) {
timer.cancel();
timer = null;
}
if (task != null) {
task.cancel();
task = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(CLASS_NAME, "onDestroy !!");
stopTimer();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android 卡死崩溃检测和拉起,App保活.zip (47个子文件)
android 卡死崩溃检测和拉起,App保活
客户端-应用A
java
com
qht
a
ANRService.java 3KB
MainActivity.java 6KB
res
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 276B
ic_launcher_round.xml 276B
menu
menu_main.xml 429B
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
values
dimens.xml 70B
colors.xml 214B
strings.xml 130B
styles.xml 728B
layout
activity_main.xml 1KB
drawable
ic_launcher_background.xml 6KB
AndroidManifest.xml 1KB
Monitor-应用B
java
com
qht
b
ConstantUtil.java 116B
PackageUtil.java 596B
MonitorService.java 6KB
SuUtil.java 1KB
MainActivity.java 2KB
res
mipmap-xxhdpi
ic_launcher_round.png 12KB
ic_launcher.png 8KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 4KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 276B
ic_launcher_round.xml 276B
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher_round.png 16KB
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 5KB
values
colors.xml 214B
strings.xml 84B
styles.xml 394B
layout
activity_main.xml 903B
drawable
ic_launcher_background.xml 6KB
AndroidManifest.xml 1KB
共 47 条
- 1
资源评论
傻傻虎虎
- 粉丝: 3755
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功