Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Push Notification by battery percentage without launching the app
    text
    copied!<p>I am trying to build a function that takes pushes notification based on the battery percentage. </p> <p>Scenario 1: The battery percentage dropped in between a range of 30% to 40% and the notification will pops out in the notification bar with some message, telling them (by notification) the user is low on battery charge.</p> <p>I have tried to do a service but to no avail. I was able to get BroadcastReceiver to retrieve the battery percentage and pops up the notification but i just can't get the service to work out.</p> <p>I got two classes, MainActivity and BatteryService</p> <p>This is what i put in my BatteryService class</p> <pre><code>public class BatteryService extends Service { int level; public BroadcastReceiver BatInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent intent) { level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); if (level == 50) { // Notify the user with notification } } }; public void onCreate() { super.onCreate(); this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getApplicationContext(), "Command=" + level + "%", Toast.LENGTH_LONG).show(); stopSelf(); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(BatInfoReceiver); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } </code></pre> <p>I got errors like</p> <pre><code>07-29 03:08:49.447: E/AndroidRuntime(1204): FATAL EXCEPTION: main 07-29 03:08:49.447: E/AndroidRuntime(1204): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in app.bayer.notificationbattery.BatteryService$1@414bc4d8 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765) 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Handler.handleCallback(Handler.java:615) 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Handler.dispatchMessage(Handler.java:92) 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Looper.loop(Looper.java:137) 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.ActivityThread.main(ActivityThread.java:4745) 07-29 03:08:49.447: E/AndroidRuntime(1204): at java.lang.reflect.Method.invokeNative(Native Method) 07-29 03:08:49.447: E/AndroidRuntime(1204): at java.lang.reflect.Method.invoke(Method.java:511) 07-29 03:08:49.447: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 07-29 03:08:49.447: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-29 03:08:49.447: E/AndroidRuntime(1204): at dalvik.system.NativeStart.main(Native Method) 07-29 03:08:49.447: E/AndroidRuntime(1204): Caused by: java.lang.NullPointerException 07-29 03:08:49.447: E/AndroidRuntime(1204): at app.bayer.notificationbattery.BatteryService$1.onReceive(BatteryService.java:25) 07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755) 07-29 03:08:49.447: E/AndroidRuntime(1204): ... 9 more </code></pre> <p>Am i doing my service or broadcastReceiver wrongly?</p> <p>EDIT: The method i placed in MainActivity to make the notification.</p> <pre><code>public void buildNotification(Context context){ NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(context); Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); builder .setSmallIcon(R.drawable.boostninja) .setContentTitle("Berocca Performance") .setContentText("Locate our store to charge your phone") .setTicker("Berocca Performance") .setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs) .setContentIntent(pendingIntent) .setAutoCancel(true); Notification notification = builder.build(); notificationManager.notify(R.drawable.ic_launcher, notification); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload