【问题标题】:My Service does not stop when the screen is OFF屏幕关闭时我的服务不会停止
【发布时间】:2017-07-16 16:41:21
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: java android


    【解决方案1】:

    首先在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
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢您的回答,它真的很有用,但是我的服务在屏幕锁定 4 分钟后停止我不明白为什么正好是 4 分钟(我做了几个测试来检查它是总是一样的东西)!您能否向我解释一下这一行(什么是 30): (ArrayList) myManager.getRunningServices(30);
    • 表示获取最新的 30 个(可能少于 30 个)正在运行的服务。如果你的服务在这个列表中,显然它正在运行。请看这个!picture
    • 如果有帮助,请采纳我的回答。祝你今天过得愉快! :-D
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多