我有一个从 Manifest 启动的服务,该服务在第一次启动时运行良好。它显示 2 个正在运行的应用程序 - 1 个进程和 1 个服务。但是当我从最近的任务列表中刷出我的应用程序时,它不会在小米设备上再次自动启动我的服务。它在联想等其他设备中成功自动启动。
这是我的服务课程:-
public class StickyService extends Service {
private static final String TAG = "StickyService";
@Override
public void onCreate() {
super.onCreate();
startReceiver();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
startMyOwnForeground();
} else {
startForeground(1, new Notification());
}
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
startTimer();
String NOTIFICATION_CHANNEL_ID = "e xample.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
sendBroadcast(new Intent("GET_BUZZER"));
//create an intent that you want to start again.
Intent intent = new Intent(getApplicationContext(), StickyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 2000, pendingIntent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e(TAG, "onTaskRemoved");
sendBroadcast(new Intent("GET_BUZZER"));
//create an intent that you want to start again.
Intent intent = new Intent(getApplicationContext(), StickyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 2000, pendingIntent);
super.onTaskRemoved(rootIntent);
}
private void startReceiver() {
RestartReceiver receiver = new RestartReceiver();
IntentFilter localIntentFilter = new IntentFilter();
localIntentFilter.addAction("GET_BUZZER");
registerReceiver(receiver, localIntentFilter);
startService(new Intent(this, StickyService.class));
}
public class RestartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Inside receiver inline class");
Intent myIntent = new Intent(context, StickyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(myIntent);
} else {
context.startService(myIntent);
}
}
}
}
我正在用 java 代码(不在清单中)注册我的广播接收器。我使用服务类来注册广播接收器。
显现 :-
<service
android:name=".ForceCloseAutoStart.StickyService"
android:process=":StickyService" />
我还在小米设备中进行了以下更改:-Settings->Battery->Manage Apps Battery Usage
仍然无法正常工作。
请帮助解决适用于此设备的任何解决方案。