I am facing a critical issue with binded service running as foreground and with notification. I am doing everything correct as mentioned by most of the tutorials and solutions I found. I have an activity which starts a service and then bind with it. In onCreate() of service, i am using startForeground() and showing notification as well. I am also returning START_STICKY in onStartCommand() but when my application gets killed i see this in my log cat ..
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}
Service is trying to restart but gets force stopped. I want service to restart if app gets killed for any reason. I know its a binded service and runs in the same process as activity but to fix it, i am already calling startService before bind and using foreground to reduce max chances of getting killed. Can anyone explain still why service gets force stopped when its trying to restart ?
My main chunk of Service class looks like this
@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
}
I am starting service and binding in onCreate() of Activity
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");
and unbinding in 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);
}