应用程序被杀后继续服务

时间:2014-07-29 14:36:52

标签: android service kill

我有两个按钮(ON和OFF)的活动:当我点击ON按钮时,服务开始。当我点击OFF按钮时,服务停止。现在,除了从“最近的应用程序”中杀死应用程序之外,我的服务没有问题,因为在这种情况下服务重新启动。我不希望它重新启动,但我希望它继续工作。该服务是START_STICKY 这是我的“服务”代码:

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    myServiceReceiver = new MyServiceReceiver();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();
}

public class LocalBinder extends Binder {
    SensorService getService() {
        return SensorService.this;
    }
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    if (!running){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MY_ACTION_FROMACTIVITY);
        registerReceiver(myServiceReceiver, intentFilter);
        running = true;

        sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),   SensorManager.SENSOR_DELAY_NORMAL);
    }
    //return super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onDestroy() {
    Log.v(TAG,"destroy");
    // TODO Auto-generated method stub
    sensorManager.unregisterListener(this);
    mNM.cancel(NOTIFICATION);
    this.unregisterReceiver(myServiceReceiver);
    super.onDestroy();
}


private void showNotification() {
    CharSequence text = getText(R.string.activity);
    Notification notification = new Notification(R.drawable.lifestyle, text, System.currentTimeMillis());

    SharedPreferences flagNot = getSharedPreferences("flagNotification", 0);
    final SharedPreferences.Editor editor = flagNot.edit();
    editor.putBoolean("flagNotification",true);
    editor.commit();

    Intent notificationIntent = new Intent(this, ActivityTab.class);
    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.app_name),text, contentIntent);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    // Send the notification.
    mNM.notify(NOTIFICATION, notification);
}


public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){
    ......
}

public class MyServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        int hostCmd = arg1.getIntExtra(CMD, 0);
        if(hostCmd == CMD_STOP){
            running = false;

            stopSelf();
        }
    }
}       

}

请帮助我吗? 非常感谢。

1 个答案:

答案 0 :(得分:3)

在您的服务中实施startForeground并向其发送持久通知。

private void startForeground() {
    int ID = 1234;
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification.Builder builder = new Notification.Builder(getBaseContext());
    builder.setContentIntent(pendIntent);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setTicker("CUSTOM MESSAGE");
    builder.setWhen(System.currentTimeMillis());
    builder.setAutoCancel(false);
    builder.setContentTitle("Test Service");
    builder.setContentText("CUSTOM MESSAGE");

    Notification notification = builder.build();

    startForeground(ID, notification);
}