Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

android - I can't receive broadcast on battery state change?

I am having the exact same problem as this post: Battery broadcast receiver doesn't work. But it seems no one has answered that question.

Here is my BroadcastReceiver:

public class BatteryLevelReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {
    Log.v("plugg", "plug change fired");
    Toast.makeText(context, " plug change fired", Toast.LENGTH_LONG).show();
        }

And here is my AndroidManifest.xml:

<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
               <intent-filter android:priority="900">
               <action android:name="android.intent.action.BATTERY_LOW" />

               </intent-filter>
           </receiver>

           <receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
               <intent-filter android:priority="900">
               <action android:name="android.intent.action.BATTERY_CHANGED" />
               </intent-filter>
           </receiver>

I have also added this line to the manifest:

<uses-permission android:name="android.permission.BATTERY_STATS"/>

But still no success!

I would really appreciate if someone could advise me what I am doing wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the documentation for ACTION_BATTERY_CHANGED:

You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers.

There you have it: you must explicitly register for it from your Java code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...