2017-07-16 87 views
0

我正在开发一个应用程序,它只有一个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; 
} 

谢谢!

回答

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

它可以判断一个服务生是not.Then更改代码:

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

我希望它能帮助你。

+0

非常感谢您的回答,这真的很有用,但是我的服务在屏幕锁定4分钟后停止,我不明白为什么正好4分钟(我做了几次测试,检查结果始终是相同的) ! (30):(ArrayList )myManager.getRunningServices(30);(ArrayList ) – Lotus

+0

这意味着获取最新的30个(可能少于30个)正在运行的服务。如果你的服务在这个列表中,显然它正在running.please看到这个![图片](https://i.stack.imgur.com/tlXit .png) –

+0

如果这有帮助,请采纳我的答案。祝你今天愉快! :-D –