我面临着一个关键问题,即绑定服务作为前台运行并带有通知。正如我找到的大多数教程和解决方案所提到的,我所做的一切都是正确的。我有一个启动服务然后与其绑定的活动。在服务的 onCreate() 中,我使用 startForeground() 并显示通知。我还在 onStartCommand() 中返回 START_STICKY 但是当我的应用程序被杀死时我在我的日志猫中看到了这一点。
Force stopping package com.example.voicerecognizersp appid=10102 user=0
I/ActivityManager( 929): Killing proc 27176:com.example.voicerecognizersp/u0a10102: force stop com.example.voicerecognizersp
W/ActivityManager( 929): Scheduling restart of crashed service com.example.voicerecognizersp/.RecordingMfccService in 5000ms
I/ActivityManager( 929): Force stopping service ServiceRecord{453068e0 u0 com.example.voicerecognizersp/.RecordingMfccService}
服务正在尝试重新启动,但被强制停止。如果应用程序因任何原因被终止,我希望服务重新启动。我知道它是一个绑定服务,并且与 Activity 在同一进程中运行,但为了修复它,我已经在绑定之前调用 startService 并使用前台来减少被杀死的最大机会。谁能解释为什么服务在尝试重新启动时被强制停止?
我的服务类的主要部分如下所示
@Override
public void onCreate() {
Log.d(TAG, "onCreate called");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting. We put an icon in the status bar.
runAsForeground();
}
/**
* Show a notification while this service is running and run as foreground so
* that OS knows that Activity depends on service and service is not a candidate
* to be killed
*/
//http://stackoverflow.com/a/28144499/1016544
private void runAsForeground() {
Intent notificationIntent = new Intent(this, MainBindingActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("VoiceRecognizerSP")
.setContentText("Service is running ...")//.build();
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int rc = super.onStartCommand(intent, flags, startId);
Log.i("LocalService", "onStartCommand Received start id " + startId + ": " + intent + "rc : " + rc );
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
//return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
// Cancel the persistent notification.
mNM.cancel(NOTIFICATION);
Log.d(TAG, "Service onDestroy() called");
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
RecordingMfccService getService() {
Log.d(TAG, "getService done");
// Return this instance of LocalService so clients can call public methods
return RecordingMfccService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind called");
return mBinder;
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind called");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind called");
return true; //to make sure next time onRebind is called
}
我正在 Activity 的 onCreate() 中启动服务和绑定
if(!isMyServiceRunning())
{
startService(new Intent(this,RecordingMfccService.class));
}
boolean result = bindService(new Intent(this, RecordingMfccService.class), mConnection, Context.BIND_AUTO_CREATE);
LocalBroadcastManager.getInstance(this).registerReceiver((receiver), new IntentFilter(RecordingMfccService.COPA_RESULT));
if(!result)
throw new RuntimeException("Unable to bind with service in onCreate");
并在 onDestroy() 中解除绑定
if (isBound ) {
Log.i(TAG, "Unbind called");
unbindService(mConnection);
isBound = false;
//Log.i(TAG, "onStopRecording Service unbinded & isbound : " + isBound);
//needToBind = true;
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(receiver);
}