Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:30:03+00:00 2026-06-15T13:30:03+00:00

In notification i try to update my battery status. I create notification with service.

  • 0

In notification i try to update my battery status. I create notification with service. But i don’t know how to update Notofication with battery status:

I also read this docs:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html

MainActivity

@Override
    public void onCreate(Bundle savedInstanceState) {

registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
..
}


private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {

        private int scale = -1;
        private int level = -1;
        private int voltage = -1;
        private int temp = -1;

        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);

            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);
            long when = System.currentTimeMillis();

            String percent = ((level * 100) / scale) + "%";
            Notification notification = new Notification(R.drawable.call, percent, when);
            /* <set your intents here> */
            mNotificationManager.notify(7331, notification);

            Log.d(TAG, "Battery level is " + level + "/" + scale + ", temp is " + temp + ", voltage is " + voltage);
        }
    };

and i am using service which create Notification like

private void showNotification() {
        Notification notification = new Notification(R.drawable.call, getString(R.string.notification_text), System.currentTimeMillis());
        Intent intent = new Intent(this, MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, getString(R.string.notification_label), getString(R.string.notification_text_short), pi);
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(7331, notification);
    }

error:

12-07 14:46:29.425: E/AndroidRuntime(25758): FATAL EXCEPTION: main
12-07 14:46:29.425: E/AndroidRuntime(25758): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.xxx.xxx.MainActivity$1@41558b80
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.os.Handler.handleCallback(Handler.java:605)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.os.Looper.loop(Looper.java:137)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.app.ActivityThread.main(ActivityThread.java:4511)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at java.lang.reflect.Method.invokeNative(Native Method)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at java.lang.reflect.Method.invoke(Method.java:511)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at dalvik.system.NativeStart.main(Native Method)
12-07 14:46:29.425: E/AndroidRuntime(25758): Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.xxx.xxx id=7331 notification=Notification(contentView=null vibrate=null,sound=null,defaults=0x0,flags=0x0)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.os.Parcel.readException(Parcel.java:1331)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.os.Parcel.readException(Parcel.java:1281)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at 
android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:317)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.app.NotificationManager.notify(NotificationManager.java:127)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.app.NotificationManager.notify(NotificationManager.java:106)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at com.xxx.xxx.MainActivity$1.onReceive(MainActivity.java:72)
12-07 14:46:29.425: E/AndroidRuntime(25758):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
12-07 14:46:29.425: E/AndroidRuntime(25758):    ... 9 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:30:04+00:00Added an answer on June 15, 2026 at 1:30 pm

    i think you need to use permission BATTERY_STATS.
    and if you want to update notification, you have to use PendingIntent.FLAG_UPDATE_CURRENT as flag in PendingActivity.getActivity

    here’s some code from my app :

    final NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    Notification note = new Notification(R.drawable.icon, text, System.currentTimeMillis());
    note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
    note.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
    
    Intent intent = null;
    PendingIntent pendingIntent = null;
    
    intent = new Intent(context, MailActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    note.setLatestEventInfo(context, "nyxdroid", text, pendingIntent);
    notificationMgr.notify(1980 + type, note);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i try to put the progress bar into the notification. but the problem is
Is there any mechanism where I can get update notification when users try to
The following code does not work. Also try this instead of getapplicationcontext. I need
I'm trying to update the progressbar which is integrated in my notification bar but
I try to get notification when the Search button is clicked in the keyboard
Notification and NotificationManager are used to create icons to be placed on the top-left
So the title is a tad ambiguous, but I'll try and give an example.
I am trying to update my UI in FirstActivity when I receive a notification
I have a service that is running on background, in some moment this service
Is there anyway to catch this notification? _serverConnectionDiedNotification. Info -- notification=NSConcreteNotification 0x2e7f70 {name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.