屏幕关闭时,我的服务不会停止

时间:2017-07-16 16:41:22

标签: java android

我正在开发一个应用程序,它只有一个Service在后台运行。我希望此服务仅在用户使用他的手机时运行,即当手机解锁时,服务记录传感器数据,当屏幕被锁定时服务停止,但问题是我的服务继续记录数据,即使屏幕关闭。

我有以下代码,我不知道问题出在哪里!

public class ScreenReceiver extends BroadcastReceiver {
private boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println(intent.getAction());
    if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
        wasScreenOn = true;
    }else{
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            wasScreenOn = true;


        }else{
            if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                System.out.println(intent.getAction());
                wasScreenOn = false;


            }
        }
    }


    Intent intent1 = new Intent(context, MyService.class);
    intent1.putExtra("statut", wasScreenOn);
    context.startService(intent1);


}

}

清单中的代码

<receiver android:name=".ScreenReceiver">
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT"/>
        </intent-filter>
    </receiver>

    <service
        android:name=".MyService"
        android:enabled="true" />

在我的服务中,我打电话给接收器

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    boolean screenOn = intent.getBooleanExtra("statut", true);
    System.out.println("statut********************************************************"+screenOn);
    if(!screenOn){
   System.out.println("END********************************************************");

        try {
            unregisterReceiver(mReceiver);
        }catch(IllegalArgumentException e){}

        SM.unregisterListener(this, Accelerometre);
        SM.unregisterListener(this, Gyroscope);
        SM.unregisterListener(this, Gravity);
        stopSelf();

    }


   return START_STICKY;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,在ScreenReceiver.java

中声明一个方法
 private static boolean isServiceWorking(Context context, String serviceClassName) {
    ActivityManager myManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    ArrayList<ActivityManager.RunningServiceInfo> runningService =
            (ArrayList<ActivityManager.RunningServiceInfo>) myManager.getRunningServices(30);
    for(int i = 0 ; i<runningService.size();i++) {
        if(runningService.get(i).service.getClassName().equals(serviceClassName)) {
            return true;
        }
    }
    return false;
}

它可以判断服务是否存活。然后更改代码:

Intent intent1 = new Intent(context, MyService.class);
intent1.putExtra("statut", wasScreenOn);
context.startService(intent1);

为:

Intent intent1 = new Intent(context, MyService.class);
intent1.putExtra("statut", wasScreenOn);
if (wasScreenOn) {// if screen on
    if (!isServiceWorking(context, MyService.class.getName())) {// the service is not working
        context.startService(intent1); // start the service
    }
} else {// if screen off
    if (isServiceWorking(context, MyService.class.getName())) {// the service is working
        context.stopService(intent1); // stop the service
    }
}

我希望它可以帮到你。