我正在尝试在广播接收器类中获取电池状态和电平,然后在通知中显示它。
public class BatteryBroadcasts extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 234234;
@Override
public void onReceive(Context context, Intent intent) {
String notiText;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Battery Alarm").setContentText(showBatteryStatus(intent));
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private String showBatteryStatus(Intent batteryStatus){
String batteryStat = new String();
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = ((float)level / (float)scale)*100;
Log.d("in notification","battery stat "+level+"scale"+scale+" "+batteryPct);
int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE,-1);
if(isCharging && usbCharge )
batteryStat="Charging on USB\n";
if(isCharging && acCharge )
batteryStat="Charging on AC\n";
if(!isCharging)
batteryStat="Discharging\n ";
return batteryStat+batteryPct+"% battery left.";
}
}
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.r3h.batteryalarm">
<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver android:name=".BatteryBroadcasts">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
当充电状态发生变化时,会调用onReceive,但无论手机是否连接,变量isCharging始终为false。等级和比例等变量始终为-1。
当我在活动类中使用它时,相同的代码非常有效。 请帮忙 。提前谢谢。
答案 0 :(得分:0)
将动作ACTION_BATTERY_CHANGED添加到意图过滤器。
<receiver android:name=".BatteryBroadcasts">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_CHANGED"/>
...
答案 1 :(得分:0)
我是这样做的:
<receiver android:name="CheckBatteryStatusReceiver" ></receiver>
然后我有这样的接收器:
public class CheckBatteryStatusReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null){
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
if (level <= 15){
Toast.makeText(context, "Batter level " + level + "% is very low. Please connect to a charger.", Toast.LENGTH_SHORT).show();
}
}
}
}
不要忘记在Activity
onCreate()中注册接收器,如:
private void setupReceiver() {
CheckBatteryStatusReceiver batteryStatusReceiver = new CheckBatteryStatusReceiver();
registerReceiver(batteryStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
然后在onPause()
中使用:
@Override
protected void onPause() {
super.onPause();
try {
if (batteryStatusReceiver != null){
unregisterReceiver(batteryStatusReceiver);
}
}catch (IllegalArgumentException e){
e.printStackTrace();
}
}
当小于15或15时,显示toast消息。如果您正在使用模拟器AVD并且您想测试它,那么我建议您应该通过Telnet连接到模拟器,如下所示:
telnet localhost 5554(emulator port(you can see that using adb devices))
然后使用:
auth key(you will find a key for emulator in the default path of you AndroidStudioProjects)
然后你可以使用:
power capacity 15
您将看到Toast
消息弹出。
希望它有所帮助!!!