1

I am trying to develop a widget which showing Battery Temperature. when i am registering a broadcast Receiver with android.intent.action.BATTERY_CHANGED

it shows some error like this.

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BATTERY_CHANGED from pid=-1, uid=10058

As far as i know there is no need of permission for broadcast . If it is in the case of an activity. it is working properly.

    Intent intent = new Intent();
    intent.setAction("android.intent.action.BATTERY");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
DKV
  • 1,767
  • 3
  • 28
  • 49

1 Answers1

1

As far as i know there is no need of permission for broadcast

Yes, there is. You are sending the broadcast. And, quoting the documentation for ACTION_BATTERY_CHANGED:

This is a protected intent that can only be sent by the system.

You are not the system; you cannot send this broadcast.

I have no idea why you are creating a PendingIntent that is trying to send this broadcast, but you will need to do something else instead. If your objective is to trigger your BroadcastReceiver from a tap on your app widget, where that receiver also happens to listen to ACTION_BATTERY_CHANGED, then just use an explicit Intent identifying the receiver when creating your PendingIntent:

PendingIntent.getBroadcast(context, 0, new Intent(this, YourReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491