4

我正在尝试在后台运行位置跟踪器。为此,我读到我需要将pendingIntent 与fusedLocationProviderClient 一起使用,这样我的应用程序将继续每x 秒查询一次用户的位置,即使应用程序已最小化/关闭。

我正在使用来自此存储库的代码https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesPendingIntent/app/src/main/java/com/google/android/gms/location/sample/ locationupdatespendingintent

我的 mapActivity 在 kotlin 中。问题是,当我使用 pendingIntent 启动 fusedLocationProviderClient 时,我似乎无法触发任何位置更新(如果我使用回调在主线程中运行它会很好,但是,这会减慢我的手机速度)。

有谁知道如何让 fusedLocationProviderClient 在我的情况下与 pendingIntent 一起工作?

我曾尝试使用intent.setAction() 中的字符串,使用我的包名“com.russ.locationalarm”并在末尾附加“.action.PROCESS_UPDATES”。我是意图的新手,所以我不确定这是否是正确的字符串。我也尝试将 PendingIntent.GetService() 切换为 PendingIntent.getBroadCast(),但似乎都不起作用。

这是我启动 fusedLocationProviderClient 的函数:

private fun startLocationUpdates() {

    // Create the location request to start receiving updates

    mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    mLocationRequest!!.setInterval(INTERVAL)
    mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)

    // Create LocationSettingsRequest object using location request
    val builder = LocationSettingsRequest.Builder()
    builder.addLocationRequest(mLocationRequest!!)
    val locationSettingsRequest = builder.build()

    val settingsClient = LocationServices.getSettingsClient(this)
    settingsClient.checkLocationSettings(locationSettingsRequest)

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
    // new Google API SDK v11 uses getFusedLocationProviderClient(this)
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return
    }

    // mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
    println("requesting location updates")
    Utils.setRequestingLocationUpdates(this,true)
    mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, getPendingIntent())
}

这是我创建pendingIntent的地方:

private fun getPendingIntent(): PendingIntent {
    // Intent intent = new Intent(this, LocationUpdatesIntentService.class);

    // intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    println("getting pending intent")
    var intent = Intent(this,LocationUpdatesIntentService::class.java)
    intent.setAction("com.russ.locationalarm.action.PROCESS_UPDATES")

    return PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
}

这是我用来接收广播或服务的两种方法

public void onReceive(Context context, Intent intent) {
    System.out.println("RECEIVED INTENT");
    Log.i("asdf","ASDF ASDF ASDF");
    if (intent != null) {
        System.out.println("INTENT NOT EQUAL TO NULL");
        final String action = intent.getAction();
        if (ACTION_PROCESS_UPDATES.equals(action)) {
            LocationResult result = LocationResult.extractResult(intent);
            if (result != null) {
                List<Location> locations = result.getLocations();
                Utils.setLocationUpdatesResult(context, locations);
                Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
                Log.i(TAG, Utils.getLocationUpdatesResult(context));
                System.out.println("ASDF ASDF ASDF ASDF");
            }
        }
    }
}

protected void onHandleIntent(Intent intent) {
    System.out.println("RECEIVED INTENT");

    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_PROCESS_UPDATES.equals(action)) {
            LocationResult result = LocationResult.extractResult(intent);
            if (result != null) {
                List<Location> locations = result.getLocations();
                Utils.setLocationUpdatesResult(this, locations);
                Utils.sendNotification(this, Utils.getLocationResultTitle(this, locations));
                Log.i(TAG, Utils.getLocationUpdatesResult(this));
            }
        }
    }
}

我的预期结果是我将触发 onHandleIntent 或 onReceive 方法之一。相反,控制台在从 getPendingIntent 函数“获取待处理意图”后不会产生任何输出。mLLocationRequest 应该每 1 或 2 秒查询一次,并且它在早些时候使用 looper 和回调调用 fusedLocationProviderClient 工作,所以我知道不是这样。

4

0 回答 0