简体   繁体   English

屏幕关闭时,我的服务没有停止

[英]My Service does not stop when the screen is OFF

I'm developing an application, which has only one Service running in the background. 我正在开发一个应用程序,该应用程序仅在后台运行一个Service。 I want this Service to run only when the user uses his phone, ie when the phone is unlocked, the Service records the sensor data and when the screen is locked the Service stops, But the problem is that my Service continues to record data even when the screen is off. 我希望此服务仅在用户使用手机时运行,即当手机解锁时,该服务记录传感器数据,并且在屏幕锁定时该服务停止,但是问题是我的服务即使在以下情况下仍会继续记录数据:屏幕关闭。

I have the following code and I do not see where the problem is! 我有以下代码,但看不到问题出在哪里!

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);


}

} }

Code in the manifest 清单中的代码

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

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

And in my service I call the receiver 在我的服务中,我打电话给接收方

@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;
}

Thank you! 谢谢!

Firstly, declare a method in ScreenReceiver.java 首先,在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;
}

It can judge a service alive or not.Then change the code: 它可以判断服务是否有效,然后更改代码:

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

to: 至:

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
    }
}

I hope it can help you. 希望对您有所帮助。

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

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