广播不会更新电池变化值

发布于 2025-01-04 12:50:37 字数 2139 浏览 0 评论 0原文

我在应用程序中创建了一个通知,以百分比形式显示电池电量。我的清单文件是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.bg"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".CreateBgActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BatteryReceive" >
        <intent-filter>

            <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>
</application>

我创建了一个单独的类,该类扩展了 BroadcastReceiver 以接收电池电量并在状态栏中发出通知。我的代码是:

public class BatteryReceive extends BroadcastReceiver {
private String bat = "android.intent.action.BATTERY_CHANGED";

@Override
public void onReceive(Context context, Intent intent) {
    //Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));
    if(intent.getAction().equals(bat)) {
    Toast.makeText(context, "Battery Level"+intent.getIntExtra("level", 0), Toast.LENGTH_SHORT).show();
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.ic_launcher,""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1),System.currentTimeMillis());
    //notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;


    notifyDetails.setLatestEventInfo(context, "Batter Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), null);
    mNotificationManager.notify(1, notifyDetails);
    }
}

 }

我无法收到电池电量的任何通知或日志输出:(我的代码有什么问题吗?有人可以帮助我解决这个问题。

I have created a notification in my application to show the battery level in percentage. My manifest file is

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.bg"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".CreateBgActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BatteryReceive" >
        <intent-filter>

            <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>
</application>

I created a separate class that extends BroadcastReceiver to receive the battery level and notify in the status bar. My code is:

public class BatteryReceive extends BroadcastReceiver {
private String bat = "android.intent.action.BATTERY_CHANGED";

@Override
public void onReceive(Context context, Intent intent) {
    //Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));
    if(intent.getAction().equals(bat)) {
    Toast.makeText(context, "Battery Level"+intent.getIntExtra("level", 0), Toast.LENGTH_SHORT).show();
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.ic_launcher,""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1),System.currentTimeMillis());
    //notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;


    notifyDetails.setLatestEventInfo(context, "Batter Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), null);
    mNotificationManager.notify(1, notifyDetails);
    }
}

 }

I could't get any notification or Log output for the battery level :( Is it anything wrong with my code. SOme one help me out of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拧巴小姐 2025-01-11 12:50:37

@覆盖
公共无效onReceive(上下文上下文,意图意图){

//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
   // This is called when Your battery is changed 

}

}

@Override
public void onReceive(Context context, Intent intent) {

//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
   // This is called when Your battery is changed 

}

}

爱,才寂寞 2025-01-11 12:50:37

更改:

if(intent.getAction().equals(bat))

至:

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))

Change:

if(intent.getAction().equals(bat))

To:

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文