【问题标题】:how to check if we already call fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIndent);如何检查我们是否已经调用了 fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIndent);
【发布时间】:2019-03-05 10:40:14
【问题描述】:

如何检查我们是否已经调用 fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIndent); ?因为如果我们再次调用它,那么任何先前注册的具有相同 PendingIntent(由 equals(Object) 定义)的请求都将被此请求替换。

【问题讨论】:

    标签: android android-pendingintent android-location android-fusedlocation


    【解决方案1】:

    FLAG_NO_CREATE 能解决问题吗?

            Intent intent = new Intent(context, Receiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
            if(pendingIntent != null){
                //requestLocationUpdates()
            }
    

    【讨论】:

    • 不,因为如果我做 fusedLocationProviderClient.removeLocationUpdates(pendingIntent);然后 fusedLocationProviderClient 不再监听任何位置更新,但pendingindent 仍然在这里:(
    • 然后您可以使用 SharedPreferences 或本地数据库中的字段跟踪进度。当您调用 requestLocationUpdates() 时,将其设置为 true,而一旦您调用 removeLocationUpdates(),将其设置为 false。您可以在应用程序的任何位置访问此字段,以检查您是否应该提出新的位置请求。
    猜你喜欢
    • 1970-01-01
    • 2021-07-17
    • 2021-08-27
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多