Android - 按电池百分比推送通知而不启动应用程序

时间:2013-07-29 03:36:05

标签: android android-service android-broadcast

我正在尝试构建一个根据电池百分比推送通知的功能。

情景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);
 }

1 个答案:

答案 0 :(得分:1)

buildNotification方法放在新类中。比方说,你称之为'Utils'。

public class Utils {

public void buildNotification(Context context){...}

}

现在从服务中调用它,并从您从原始活动中调用它:

Utils utils = new Utils();
utils.buildNotification(<context argument goes in here>);

现在您已经从Activity中重构了它,并且不必为了访问它而创建Activity的对象。