我正在尝试构建一个基于电池百分比接收推送通知的功能。
场景 1:电池百分比下降在 30% 到 40% 之间,通知栏中会弹出一些消息,告诉他们(通过通知)用户电池电量不足。
我曾尝试提供服务,但无济于事。我能够让 BroadcastReceiver 检索电池百分比并弹出通知,但我无法让服务正常工作。
我有两个类,MainActivity 和 BatteryService
这就是我放在 BatteryService 类中的内容
public class BatteryService extends Service {
int level;
public BroadcastReceiver BatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (level == 50)
{
// Notify the user with notification
}
}
};
public void onCreate() {
super.onCreate();
this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Command=" + level + "%", Toast.LENGTH_LONG).show();
stopSelf();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(BatInfoReceiver);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
我遇到了类似的错误
07-29 03:08:49.447: E/AndroidRuntime(1204): FATAL EXCEPTION: main
07-29 03:08:49.447: E/AndroidRuntime(1204): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in app.bayer.notificationbattery.BatteryService$1@414bc4d8
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Handler.handleCallback(Handler.java:615)
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.os.Looper.loop(Looper.java:137)
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 03:08:49.447: E/AndroidRuntime(1204): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 03:08:49.447: E/AndroidRuntime(1204): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 03:08:49.447: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 03:08:49.447: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 03:08:49.447: E/AndroidRuntime(1204): at dalvik.system.NativeStart.main(Native Method)
07-29 03:08:49.447: E/AndroidRuntime(1204): Caused by: java.lang.NullPointerException
07-29 03:08:49.447: E/AndroidRuntime(1204): at app.bayer.notificationbattery.BatteryService$1.onReceive(BatteryService.java:25)
07-29 03:08:49.447: E/AndroidRuntime(1204): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
07-29 03:08:49.447: E/AndroidRuntime(1204): ... 9 more
我做错了我的服务或广播接收器吗?
编辑:我放置在 MainActivity 中以发出通知的方法。
public void buildNotification(Context context){
NotificationManager notificationManager
= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder
.setSmallIcon(R.drawable.boostninja)
.setContentTitle("Berocca Performance")
.setContentText("Locate our store to charge your phone")
.setTicker("Berocca Performance")
.setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Notification notification = builder.build();
notificationManager.notify(R.drawable.ic_launcher, notification);
}