Android 服务在刷出应用程序时被终止
Android service gets killed on swiping out the application
我有一个 Android 服务 class 其代码如下:
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);
然而,每当 Activity 从任务列表(最近)中滑出时,服务也会停止。我过度使用 onTaskRemoved
来补救它,但它似乎仍然不起作用,而且 AlarmManager 从未启动 pendingIntent
。我尝试使用这两种方法:set
和 setExact
用于 AlarmManager
.
我还尝试将以下选项添加到 <service>
标签
android:stopWithTask="false"
android:process=":remote"
但无济于事。
我在这里做错了什么?感谢您的帮助。
这与您的方法不同,但我最近通过在服务 运行
时添加通知来解决此问题
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 方法中取消。
如果您不想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
我终于找到了自己问题的答案。这似乎是我 phone (Mi UI) 上 运行 的 android 的特殊风味的问题。关于是否需要允许重新启动每个应用程序都有一个单独的设置。
除非配置此设置,否则任何更改权限和设置警报都对我没有帮助。
如果您在某些设备上尝试此操作,遗憾的是,它不会起作用。
一些 OEM 决定更改当您从最近的任务中删除应用程序时发生的正常行为,因此它们会处于半禁用状态:
我有一个 Android 服务 class 其代码如下:
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);
然而,每当 Activity 从任务列表(最近)中滑出时,服务也会停止。我过度使用 onTaskRemoved
来补救它,但它似乎仍然不起作用,而且 AlarmManager 从未启动 pendingIntent
。我尝试使用这两种方法:set
和 setExact
用于 AlarmManager
.
我还尝试将以下选项添加到 <service>
标签
android:stopWithTask="false"
android:process=":remote"
但无济于事。
我在这里做错了什么?感谢您的帮助。
这与您的方法不同,但我最近通过在服务 运行
时添加通知来解决此问题 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 方法中取消。
如果您不想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
我终于找到了自己问题的答案。这似乎是我 phone (Mi UI) 上 运行 的 android 的特殊风味的问题。关于是否需要允许重新启动每个应用程序都有一个单独的设置。
除非配置此设置,否则任何更改权限和设置警报都对我没有帮助。
如果您在某些设备上尝试此操作,遗憾的是,它不会起作用。
一些 OEM 决定更改当您从最近的任务中删除应用程序时发生的正常行为,因此它们会处于半禁用状态: