두 버튼 (켜기 및 끄기)으로 활동하고 있습니다 : 켜기 버튼을 클릭하면 서비스가 시작됩니다. OFF botton을 클릭하면 서비스가 중단됩니다. 이제는 "최근 앱"에서 앱을 죽일 때와 별개로 문제가 발생하지 않습니다.이 경우 서비스가 다시 시작되기 때문입니다. 다시 시작하지 않으려 고하지만 계속 작동하게하고 싶습니다. 서비스는 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();
}
}
}
}
당신이 좀 도와 주 시겠어요?
많은 감사.
우리는 개발자입니다. OS 작동을 방해 할 수는 없습니다. 앱이 죽을 때 OS는 백그라운드 서비스를 자동으로 모두 중지해야 메모리를 비울 수 있습니다. 앱이 강제 종료 될 때까지 서비스를 계속할 수 있습니다. – Ranjit
나는 그것을 안다. 내가 원하는 것을 더 잘 설명하기 위해, 예를 들어 Google 시계의 카운트 다운을 예로들 수 있습니다. 앱이 시작되고 최근 앱에서 앱을 종료하면 카운트 다운이 처음부터 다시 시작되지 않지만 계속됩니다. – Grancein
두 가지 레벨이 있습니다. 하나는 서비스가 죽는 것을 막고, 완전히 다른 서비스는 사용자에게 투명한 방식으로 살해 된 서비스가 중단 된 곳에서 * 대체 * 서비스를 선택할 수있게합니다. –