我的前台服务在杀死应用程序后在vivo等某些设备上被杀死?
My foreground service is killed on some devices like vivo after killing app?
我的前台服务在杀死应用程序后在某些设备(如 vivo)上被杀死,是否有任何解决方法可以使其保持活动状态?
我使用的前台服务如下:
public class MyService extends IntentService {
private final String TAG = "IntentService";
private PowerManager.WakeLock wakeLock;
public MyService() {
super("MyService");
setIntentRedelivery(true);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d(TAG, "onHandleIntent: Service running");
for (int i = 0; i < 20; i++) {
Log.d(TAG, "onHandleIntent: service running status: " + i);
SystemClock.sleep(3000);
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate: IntentService Created");
super.onCreate();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"Service: WakeLock");
this.wakeLock.acquire();
Log.d(TAG, "onCreate: Wakelock acquired");
Notification notification = new NotificationCompat.Builder(this, App.NOTIFICATION_CHANNEL_ID)
.setContentTitle("Intent Service")
.setContentText("Service running in background")
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.build();
startForeground(12, notification);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: IntentService Destroyed");
super.onDestroy();
this.wakeLock.release();
Log.d(TAG, "onDestroy: Wakelock released");
}
}
我使用变通方法解决了这个问题。
像这样注册了一个假的静态隐式接收器:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".SampleBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我的示例广播文件:
public class SampleBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d("Foreground App", "Boot Completed");
}
}
}
这将我的应用置于 OS 的自动启动部分。
现在,当我开始我的服务时,即使我终止了该应用程序,它也是 运行。
我的前台服务在杀死应用程序后在某些设备(如 vivo)上被杀死,是否有任何解决方法可以使其保持活动状态?
我使用的前台服务如下:
public class MyService extends IntentService {
private final String TAG = "IntentService";
private PowerManager.WakeLock wakeLock;
public MyService() {
super("MyService");
setIntentRedelivery(true);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d(TAG, "onHandleIntent: Service running");
for (int i = 0; i < 20; i++) {
Log.d(TAG, "onHandleIntent: service running status: " + i);
SystemClock.sleep(3000);
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate: IntentService Created");
super.onCreate();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"Service: WakeLock");
this.wakeLock.acquire();
Log.d(TAG, "onCreate: Wakelock acquired");
Notification notification = new NotificationCompat.Builder(this, App.NOTIFICATION_CHANNEL_ID)
.setContentTitle("Intent Service")
.setContentText("Service running in background")
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.build();
startForeground(12, notification);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: IntentService Destroyed");
super.onDestroy();
this.wakeLock.release();
Log.d(TAG, "onDestroy: Wakelock released");
}
}
我使用变通方法解决了这个问题。
像这样注册了一个假的静态隐式接收器:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".SampleBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我的示例广播文件:
public class SampleBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d("Foreground App", "Boot Completed");
}
}
}
这将我的应用置于 OS 的自动启动部分。
现在,当我开始我的服务时,即使我终止了该应用程序,它也是 运行。