Android的服务被刷上了应用打死上了、Android

2023-09-07 17:49:54 作者:你是我不愿戒掉的毒瘾

我有一个Android服务类code,对于它如下:

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 。这将启动该服务的活动有以下code:

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

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

不过,只要该活动从任务列表中偷走了(最近)服务也被停止。我在骑了 onTaskRemoved 来纠正它,但它似乎仍然没有工作和AlarmManager从未启动 pendingIntent 。我已经使用这两种方法的尝试:设置 setExact 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"

但无济于事。

but to no avail.

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

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

推荐答案

我终于找到了答案,以我自己的问题。看来这是与Android的,我是在我的手机(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.