【发布时间】:2019-08-20 20:27:19
【问题描述】:
我有一个即时按摩器的后台服务,它会导致大量崩溃(每天大约 10.000 次)。
我做了什么:我用onStartCommant 启动服务并返回START_STICKY。
onDestroy() 和 onTaskRemoved() 调用一个广播来重新启动服务,因此它会更新死亡,这主要是有效的。
现在我认识到 android 设备管理器给出了将其设置为待机的建议,因为崩溃太多了(在后台用户不会注意到它)。
这是我的服务:
public static boolean isMyServiceRunning(Class<?> serviceClass, Context c) {
ActivityManager manager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public static MassageDataSource getMassageDataSource() {
return massageDataSource;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (context == null) {
context = this;
}
SQLiteDatabase.loadLibs(context);
if (massageDataSource == null) {
massageDataSource = new MassageDataSource(context);
massageDataSource.open();
}
startTimer();
return START_STICKY;
//return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new Intent(context, RestartService.class);
sendBroadcast(broadcastIntent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent broadcastIntent = new Intent(context, RestartService.class);
sendBroadcast(broadcastIntent);
}
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
Log.i("in timer", "in timer ++++ " + (counter++));
}
};
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
这是我的广播:
@Override
public void onReceive(Context context, Intent intent) {
Log.i(RestartService.class.getSimpleName(), "Service Stopped!");
if (!BackgroundService.isMyServiceRunning(BackgroundService.class, context)) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent service = new Intent(context, BackgroundService.class);
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
pendingIntent = PendingIntent.getForegroundService(context, BackgroundService.SERVICE_ID, service, PendingIntent.FLAG_CANCEL_CURRENT);
} else {
pendingIntent = PendingIntent.getService(context, BackgroundService.SERVICE_ID, service, PendingIntent.FLAG_CANCEL_CURRENT);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervall, pendingIntent);
Log.i(RestartService.class.getSimpleName(), "Service restarted.");
} else {
Log.i(RestartService.class.getSimpleName(), "Service is already running");
}
}
服务运行 8 秒,然后被广播停止并重新启动。但是每次它被破坏时都会崩溃并在 logcat 上产生这个输出:
Thread[3,tid=17948,WaitingInMainSignalCatcherLoop,Thread*=0x75b3a16400,peer=0x13b80020,"Signal Catcher"]: reacting to signal 3
Wrote stack traces to '[tombstoned]'
D/ConnectivityManager_URSP: Ursp sIsUrsp=false, sIsCheckUrsp=false, uid=10291
D/Proxy: urspP is null: 10291
I/LoadedApk: No resource references to update in package com.test.module1
I/LoadedApk: No resource references to update in package com.test.module2
D/SensorManager: registerListener :: 10, TMD4906 lux Sensor, 66667, 0,
I/RestartService: Service Stopped!
I/RestartService: Service restarted.
我读到START_STICKY 导致了这种情况,所以我尝试了其他命令,但只有START_STICKY 我的服务不会死。
此外,由于某些原因,服务未在启动时启动。
【问题讨论】:
-
我不确定,但 timerTask 可能是原因。为什么不取消 onDestroy 中的任务,看看有什么不同?
-
我试过了,但没有改变
标签: java android service crash