2017-07-16 53 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 –

相關問題