简体   繁体   English

Android:在后台服务中打开/关闭屏幕时帮助计数

[英]Android: Help counting when the screen is turning off/on in a background service

For a little app that I'm working at I need to count how many times the screen turns off and on and when it does that. 对于我正在使用的一个小应用程序,我需要计算屏幕关闭和打开的次数以及何时关闭。 I created a BroadcastReceiver that runs a Service after booting that is supposed to count that. 我创建了一个BroadcastReceiver,它应该在启动后运行一个应该计数的服务。

This is my Service class: 这是我的服务班级:

public class MyService extends Service {

  private final IBinder mBinder = new MyBinder();
  public static boolean wasScreenOn = true;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //the next line is line 29
      if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.i("Screen off", String.valueOf(System.currentTimeMillis()));
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("Screen on", String.valueOf(System.currentTimeMillis()));
            wasScreenOn = true;
        }
      return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent arg0) {
  //TODO for communication return IBinder implementation
        return mBinder;
  }

  public class MyBinder extends Binder {
        MyService getService() {
          return MyService.this;
        }
      }

} }

The problem is that I get this error: 问题是我收到此错误:

06-26 23:16:38.485: E/AndroidRuntime(1402): FATAL EXCEPTION: main
06-26 23:16:38.485: E/AndroidRuntime(1402): java.lang.RuntimeException: Unable to start service com.MyApp.MyService@40ce0078 with Intent { cmp=com.MyApp/.MyService (has extras) }: java.lang.NullPointerException
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.access$1900(ActivityThread.java:141)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.os.Looper.loop(Looper.java:137)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at dalvik.system.NativeStart.main(Native Method)
06-26 23:16:38.485: E/AndroidRuntime(1402): Caused by: java.lang.NullPointerException
06-26 23:16:38.485: E/AndroidRuntime(1402):     at com.MyApp.MyService.onStartCommand(MyService.java:29)
06-26 23:16:38.485: E/AndroidRuntime(1402):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)

I imagine it is because when the Service starts there is still no Intent for Screen On/Off. 我想这是因为当服务启动时,仍然没有打开/关闭屏幕的意图。 But I want this Service to run in the background and wait for it. 但是我希望该服务在后台运行并等待它。

How should I do this? 我应该怎么做?

Thanks a lot! 非常感谢!

Your problem is NullPointerException in onStartCommand . 您的问题是onStartCommand NullPointerException The intent is null . intentnull

From the documentation : 文档中

intent The Intent supplied to startService(Intent), as given. intent提供给startService(Intent)的Intent。 This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY. 如果服务在其进程消失后正在重新启动,并且它先前已返回除START_STICKY_COMPATIBILITY以外的任何内容,则该值为null。

You should check for null value before you do anything with the intent . 在执行intent之前,应检查null值。

I made it work after I used the code from this app 我使用了此应用程序中的代码后,它就开始工作了

Code: 码:

public class HardwareTriggerService extends Service {
private HardwareTriggerReceiver hardwareTriggerReceiver;
@Override
public void onCreate() {
    super.onCreate();
    Log.e(">>>>>>", "HardwareTriggerService CREATED.");
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    hardwareTriggerReceiver = new HardwareTriggerReceiver();
    registerReceiver(hardwareTriggerReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(">>>>>>", "HardwareTriggerService DESTROYED.");
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);

    unregisterReceiver(hardwareTriggerReceiver);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM