Нужно сделать чтобы мой Service не убивался системой и и другими чистилками мусора.
Сам код:
Manifest.xml
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETE"
android:process=":BatMonitoring">
</service>
<receiver
android:name=".BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentService = new Intent(this,NotificationService.class);
startService(intentService);
}
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, NotificationService.class);
context.startService(serviceIntent);
}
}
NotificationService.java
public class NotificationService extends Service {
private NotificationManager notificationManager;
public static final int DEFAULT_NOTIFICATION_ID = 101;
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
this.registerReceiver(this.mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int chargeState = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
switch (chargeState) {
case BatteryManager.BATTERY_STATUS_CHARGING:
if (isCharging) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this)
.setContentTitle("Charging...")
.setContentText("Battery level is: " + Integer.toString(level))
.setSmallIcon(android.R.drawable.stat_notify_chat);
Notification notification = builder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
startForeground(DEFAULT_NOTIFICATION_ID, notification);
}
break;
case BatteryManager.BATTERY_STATUS_FULL:
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
break;
}
}
};
@Override
public void onDestroy () {
super.onDestroy();
notificationManager.cancel(DEFAULT_NOTIFICATION_ID);
this.unregisterReceiver(this.mBatteryInfoReceiver);
//Disabling service
stopSelf();
}
public IBinder onBind(Intent intent) {
return null;
}
}
Одним из примеров брал этот вопрос ,но все равно не работает как должно.
Буду очень благодарен любой помощи!
UPD: Окончательно вопрос не решил, но если кому нужно, то добавление перезапуска сервиса в void onDestroy () и в onTaskRemoved(Intent rootIntent) частично помогло.