在刷掉应用程序时,Android服务会被杀死

时间:2015-04-13 14:27:37

标签: android service

我有一个Android服务类,其代码如下:

public class LoginService extends Service {
BroadcastReceiver wifiStateChangeReciever;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("AndroidLearning", "Service onStartCommand Started.");
    return Service.START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i("AndroidLearning", "Service Started.");

    final IntentFilter intentFilter = new IntentFilter();
    // intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
    intentFilter.addAction("android.net.wifi.STATE_CHANGE");
    wifiStateChangeReciever = new WifiStateChangeReciever();
    this.registerReceiver(wifiStateChangeReciever, intentFilter, null, null);

    Log.i("AndroidLearning", "Reciever Registered.");
}

@Override
public void onDestroy() {
    Log.i("AndroidLearning", "Service Destroyed.");
    this.unregisterReceiver(wifiStateChangeReciever);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.w("AndroidLearning", "On Task Remove: FLAG_STOP_WITH_TASK - "
            + ServiceInfo.FLAG_STOP_WITH_TASK);
    this.unregisterReceiver(wifiStateChangeReciever);
    Intent restartServiceIntent = new Intent(getApplicationContext(),
    this.getClass()); restartServiceIntent.setPackage(getPackageName());
    PendingIntent restartServicePendingIntent = PendingIntent.getService(
            getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    alarmService.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); 
    Log.w("AndroidLearning", "End on task removed");
    super.onTaskRemoved(rootIntent);

}
}

注册BroadcastReciever。启动此服务的Activity具有以下代码:

Intent intent = new Intent(this, LoginService.class);
startService(intent);

但是,每当从任务列表(最近)中删除活动时,服务也会停止。我过度使用onTaskRemoved来解决它,但它似乎仍然不起作用,并且AlarmManager永远不会启动pendingIntent。我尝试对set使用这两种方法:setExactAlarmManager

我还尝试将以下选项添加到<service>代码

android:stopWithTask="false"
android:process=":remote"

但无济于事。

我在这里做错了什么?谢谢你的帮助。

4 个答案:

答案 0 :(得分:5)

我终于找到了自己问题的答案。这似乎是我在手机(Mi UI)上运行的特定Android风格的问题。每个应用程序都有一个单独的设置,是否需要重新启动。

Screenshot - autostart is the setting that I was looking for

除非配置了此设置,否则任何更改权限和设置警报的数量都无法帮助我。

答案 1 :(得分:1)

这是与您不同的方法,但我最近通过在服务运行时添加通知来修复此问题

  private void showNotification(){
    NotificationCompat.Builder builer = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Service active")
            .setContentText("Your service keeps running")
            .setOngoing(true);
    mNotificationManager.notify(NOTIFICATION_ID, builer.build());

}

通知显示在onStartCommand中,并在服务ondestroy方法中解除。

答案 2 :(得分:1)

如果你不想让android关闭它,你需要在前台启动服务。

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
    System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
    getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

http://developer.android.com/guide/components/services.html#Foreground

答案 3 :(得分:0)

遗憾的是,如果您在某些设备上尝试此操作,则将无法正常工作。

一些OEM决定更改当您从最近的任务中删除应用程序时所发生的行为的正常行为,因此它们变为半禁用: