尽管在前台运行并且使用START_STICKY,但无法在被杀后重新启动服务

时间:2015-03-13 02:45:45

标签: android service bind persistent foreground-service

我正面临一个关键问题,即绑定服务作为前台运行并带有通知。正如我发现的大多数教程和解决方案所提到的,我正在做的一切都是正确的。我有一个活动,它启动一个服务然后绑定它。在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}

服务正在尝试重新启动但强行停止。如果应用程序因任何原因被杀,我希望服务重新启动。我知道它是一个绑定服务并且在与活动相同的过程中运行但是为了修复它,我已经在绑定之前调用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);



        }

0 个答案:

没有答案