I decided to write an application that sends a notification at a certain level of charge. But the problem is that when you close the application, the service also closes and notifications do not come:

AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETE"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <service android:name=".NotificationService" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETE"> </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> 

BootReciever.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 static final int DEFAULT_NOTIFICATION_ID = 101; @Override public void onCreate() { super.onCreate(); this.registerReceiver(this.mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } public int onStartCommand(Intent intent, int flags, int startId) { return Service.START_STICKY; } BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, 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); final 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; } } }; public IBinder onBind(Intent arg0) { return null; } @Override public void onDestroy() { super.onDestroy(); this.unregisterReceiver(this.mBatteryInfoReceiver); stopSelf(); } 

}

I would be very grateful for any help or hint.

    1 answer 1

    You need to make your mBatteryInfoReceiver a separate class and register it in the manifest with the ACTION_BATTERY_CHANGED action