Android 服务在刷出应用程序时被杀死 [英] Android service gets killed on swiping out the application

查看:14
本文介绍了Android 服务在刷出应用程序时被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Android Service class the code for which is as follows:

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 具有以下代码:

It registers a BroadcastReciever. The Activity which starts this service has the following code:

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

但是,每当 Activity 从任务列表(最近)中滑出时,该服务也会停止.我过度使用 onTaskRemoved 来修复它,但它似乎仍然不起作用,并且 AlarmManager 从未启动 pendingIntent.我尝试使用两种方法:setsetExact 用于 AlarmManager.

However whenever the Activity is swiped out from the task list (recent) the service is also stopped. I over rode the onTaskRemoved to remedy it but it still does not seem to work and the AlarmManager never starts the pendingIntent. I have tries using both method: set and setExact for the AlarmManager.

我还尝试将以下选项添加到 标签

I also tried adding the following options to <service> tags

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

但无济于事.

我在这里做错了什么?感谢您的帮助.

What am I doing wrong here? Thanks for the help.

推荐答案

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

I finally found the answer to my own problem. It seems this was a problem with the particular flavor of android that I was running on my phone (Mi UI). There was a separate setting regarding each application whether it needed to be allowed to be restarted or not.

除非配置此设置,否则更改权限和设置警报对我没有任何帮助.

Unless this setting is configured no amount of changing permissions and setting Alarms helped me.

这篇关于Android 服务在刷出应用程序时被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆