Android-在不启动应用程序的情况下按电池百分比推送通知

Android-在不启动应用程序的情况下按电池百分比推送通知,android,android-service,android-broadcast,Android,Android Service,Android Broadcast,我正在尝试构建一个基于电池百分比接收推送通知的函数 情景1: 电池百分比下降到30%到40%之间,通知将在通知栏中弹出,并显示一些消息,告诉他们(通过通知)用户电池电量不足 我试过做一项服务,但没有用。我能够让BroadcastReceiver检索电池百分比并弹出通知,但我无法让服务正常工作 我有两门课,主要活动和电池服务 这是我在我的BatteryService课程中的内容 public class BatteryService extends Service { int level; pu

我正在尝试构建一个基于电池百分比接收推送通知的函数

情景1: 电池百分比下降到30%到40%之间,通知将在通知栏中弹出,并显示一些消息,告诉他们(通过通知)用户电池电量不足

我试过做一项服务,但没有用。我能够让BroadcastReceiver检索电池百分比并弹出通知,但我无法让服务正常工作

我有两门课,主要活动和电池服务

这是我在我的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);
 }

buildNotification
方法放入新类中。比方说,你称之为“乌蒂尔斯”

public class Utils {

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

}
现在,从服务和您的原始活动中这样称呼它:

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

现在您已经从活动中重构了它,并且不必创建活动的对象来访问它

啊,我刚刚意识到我在这一行中犯了一个错误,我在MainActivity中调用了我的方法,它是buildNotification(上下文上下文),我在BatteryService中用MainActivity.buildNotification(MainActivity)调用了它,其中MainActivity MainActivity。我如何更正它?您不应该为了访问活动对象的方法而创建活动对象。创建一个Utils类,将代码从您的活动中取出并放入其中。我对Utils有点陌生。你是怎么做的?请编辑问题并添加你想访问的相关方法。