4

Googleでモバイルトラッキングアプリを開発中です。位置更新のために、Fused location APIが高精度で優先度として使用されます。画面がオフのときにAndroidのバックグラウンドの場所の更新が行われない

画面がオフの場合でも、場所の更新が必要です。したがって、背景はServiceです。背景Serviceも部分WakeLockを取得しているため、デバイスはスリープ状態にはなりません。 Serviceのバックグラウンドでは、保留中のアップデートがServiceであるロケーションアップデートをリクエストしています。

問題は、画面がオンになっているときにのみ位置情報の更新を受信して​​いることです。画面がオフになるとすぐに、位置の更新が停止します。 Serviceによって実行されるThreadも存在します。

BroadcastReceiverによって画面がオフになったときに再度位置情報要求を作成することもできません。

private static final String TAG = "RouteExecution"; 

private static RouteExecution routeExecution = null; 

private static GoogleApiClient mGoogleApiClient; 
private static PendingIntent pendingIntent = null; 
private PowerManager.WakeLock waitLock; 
/** 
* Creates an IntentService. Invoked by your subclass's constructor. 
*/ 
public RouteExecution() { 
    super(TAG); 

    RouteExecution.routeExecution = this; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    mGoogleApiClient.connect(); 

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    BroadcastReceiver mReceiver = new PowerButtonReceiver(); 
    registerReceiver(mReceiver, filter); 

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    waitLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); 
    waitLock.acquire(); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 

    if (LocationResult.hasResult(intent)) { 

     LocationResult locationResult = LocationResult.extractResult(intent); 
     Location location = locationResult.getLastLocation(); 

     GPSLocation gpsLocation = new GPSLocation(location); 

     Log.d(TAG, "Location Accuracy: " + location.getAccuracy() + " " 
       + " has: " + location.hasAccuracy() + " Provider: " + location.getProvider() 
       + " long: " + location.getLongitude() + " lat: " + location.getLatitude()); 
    } 
} 



public boolean checkLocationPermission() { 

    if (ActivityCompat.checkSelfPermission(routeExecution, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
      && ActivityCompat.checkSelfPermission(routeExecution, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return false; 
    } 

    return true; 
} 

public void createLocationListener() { 

    if (!this.checkLocationPermission()) { 
     return; 
    } 

    LocationRequest mLocationRequest = LocationRequest.create(); 
    mLocationRequest.setInterval(5000); 
    mLocationRequest.setFastestInterval(5000); 
    mLocationRequest.setSmallestDisplacement(0); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    PendingResult<Status> statusPendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, 
      mLocationRequest, pendingIntent); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    Log.d(TAG, mGoogleApiClient.isConnected() + " On Connected"); 
    synchronized (this) { 

      createLocationListener(); 
    } 
} 


public static GoogleApiClient getmGoogleApiClient() { 
    return mGoogleApiClient; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 


    waitLock.release(); 
    mGoogleApiClient.disconnect(); 
} 

public static RouteExecution getRouteExecution() { 
    return routeExecution; 
} 

public static void setPendingIntent(PendingIntent pendingIntent) { 
    RouteExecution.pendingIntent = pendingIntent; 
} 

ServiceAlarmManagerを使用して開始されます。

はここで背景Serviceクラス(RouteExecution)です。ここで抽出は次のとおりです。

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
Intent updateServiceIntent = new Intent(context, RouteExecution.class); 
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0); 
RouteExecution.setPendingIntent(pendingUpdateIntent); 
alarmManager.set(AlarmManager.RTC_WAKEUP, 50000, pendingUpdateIntent); 

BroadcastReceiver:画面がオフの場合でも

public class PowerButtonReceiver extends BroadcastReceiver { 
    private static final String TAG = "PowerButton"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.d(TAG, "Power Button"); 
     if (RouteExecution.getRouteExecution() != null) { 
      RouteExecution.getRouteExecution().createLocationListener(); 
     } 
    } 
} 

場所を得続けるためにどのように更新します。

ありがとうございました。

+1

「サービス」をどうやって始めますか? 'Service'と' BroadcastReceiver'の両方のコードを表示してください。 – earthw0rmjim

+0

同様にhttp:// stackoverflowもチェックしてください。com/questions/17613888/locationclient-doesnt-give-callback-when-screen-light-go-off-but-my-wakefulth – abbath

答えて

0

まず、をServiceに入力すると、信頼性がまったく得られません。そのコードが実行される前に、デバイスはスリープ状態に戻ることができます。

私はWakeLockが技術的にonReceive()時に保証されているので、代わりにPendingIntent.getService()PendingIntent.getBroadcast()を呼び出し、そこからServiceを開始します。

のどちらか、onReceive()WakeLockをAQUIREあなたServiceを開始し、適切な

または

は、そのロジックのほとんどを実装しなくても、基本的には同じことをしている、WakefulBroadcastReceiverを使用する場合、ServiceからWakeLockをリリースあなた自身。

もう1つの問題は、マシュマロデバイスでAlarmManager.set()を使用していることです。 Dozeがアクティブの場合、この方法で設定された

アラームは、これらのデバイス上で解雇取得することはできません。

利用setAndAllowWhileIdle()の代わりに、APIレベル23+でset()

関連する問題