Android Google Play服务:如何检查当前是否有;主动的;注册到位置更新/活动识别的挂起意图回调?

Android Google Play服务:如何检查当前是否有;主动的;注册到位置更新/活动识别的挂起意图回调?,android,google-play-services,android-pendingintent,android-location,activity-recognition,Android,Google Play Services,Android Pendingintent,Android Location,Activity Recognition,我的应用程序在后台执行定期位置更新和活动识别检测 我正在使用Google Play Services API实现这一点: 例如,为了注册到位置更新,我提供了接收更新的挂起意图: mLocationClient.requestLocationUpdates(mLocationRequest, pendingInent); 要注销位置更新,我将执行以下操作: mLocationClient.removeLocationUpdates(pendingInent); 这很好,而且效果很好 但我如何才

我的应用程序在后台执行定期位置更新和活动识别检测

我正在使用Google Play Services API实现这一点:

例如,为了注册到位置更新,我提供了接收更新的挂起意图:

mLocationClient.requestLocationUpdates(mLocationRequest, pendingInent);
要注销位置更新,我将执行以下操作:

mLocationClient.removeLocationUpdates(pendingInent);
这很好,而且效果很好

但我如何才能确定我的应用程序组件当前是否有挂起的内容持有意图在Google play服务前注册以接收位置更新?

我注意到没有API来检查这一点,而且API对google play服务似乎不起作用-即使在我调用
RemovelociationUpdate()
方法之后,挂起的意图仍然存在

我知道我可以保存状态(到共享首选项),指示我是否已注册,但这不是正确的解决方案,而且会出现“错误”,因为谷歌播放进程可能会宕机,丢失挂起内容,但我的进程仍会认为位置更新处于“活动”状态

活动识别更新也存在同样的问题。

我只想说清楚,我所要做的就是让我的应用程序的用户知道我的应用程序当前是否在后台收集数据,并为他们提供一种在这两者之间切换的方法。所以,如果有其他可靠的方法来做到这一点,我也会把它作为一个答案

可靠方式=知道当前未决的意图是否真的从google play服务中注册

  • 使用
    LocationListener
    对我来说不是一个选项,因为我必须能够在进程停止时接收更新事件-只有使用
    PendingEvent
    回调才有可能
提前谢谢

更新

这是我提供的注册和注销位置更新的待定内容:

Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_UPDATE_CURRENT);
这就是我试图检查(未成功)它是否注册的方式:

Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_NO_CREATE);
boolean isLocationUpdatesEnabled = (pendingIntent != null);
isLocationUpdateEnabled
在调用
RemovelocationUpdate()后返回true事件

我只想说清楚,我所要做的就是向我的 应用程序能够知道我的应用程序当前是否正在中收集数据 并为他们提供一种在这两者之间切换的方法。所以 如果有其他可行的方法可以做到这一点,我会把它作为一个例外 答案也是

通过状态栏中的自定义通知通知用户应用程序正在收集背景数据。通知会将用户带到可以正常停止录制的位置。

下面的示例是服务的一部分。启动和停止由传递用于启动服务的布尔值控制。当你的应用程序暂停时,服务仍在记录背景数据,通知会将用户带回你的应用程序,以正常关闭背景数据记录

录制startForeground时显示状态栏通知。在不录制stopForeground时(true),删除通知

boolean isRecording = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        // intent is processing = b
        boolean b = intent.getBooleanExtra(SettingRecord.RECORDING,
                isRecording);
        //custom method to start recording that changes the isRecording flag.
        startRecording(b);
    }

    if (isRecording) {
        startForeground(R.id.action_record, getCustomNotification());
        return Service.START_STICKY;
    } else {
        stopForeground(true);
        return Service.START_NOT_STICKY;
    }
}


private static Notification getCustomNotification() {
    Context context = weakContext.get();
    if (context == null) {
        return null;
    } else {
        // calculate the progress.
        int progress = numberOfLocations * 100 / MAX_LOCATIONS;
        if (progress > 100) {
            progress = 100;
        }

        String trackedMsg;
        if (isMiles) {
            trackedMsg = "Tracked "
                    + decimalFormat.format(distance * METERS_TO_MILES)
                    + " miles";
        } else {
            trackedMsg = "Tracked "
                    + decimalFormat.format(distance * METERS_TO_KILOMETERS)
                    + " km";
        }

        // build the notification.

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
        // notification 1
                .setContentTitle("Best Rides")
                // notification 2
                .setLargeIcon(
                        BitmapFactory.decodeResource(
                                context.getResources(),
                                R.drawable.ic_launcher))
                // notification 4 number of locations
                .setContentInfo(trackedMsg)
                // notification 5
                .setSmallIcon(R.drawable.ic_launcher2)
                // notification 6 time of locations

                // user can't cancel the recording message because it
                // contiually
                // updates
                .setOngoing(isRecording)
                // autocancel the notification so it appears the servcice
                // has
                // stopped
                .setAutoCancel(true);

        // notification 3 name of route
        if (progress == 100) {
            mBuilder.setTicker("Tracker Full")
                    // show the route recorder is full
                    .setContentText("Tracker Full")
                    .setPriority(NotificationCompat.PRIORITY_LOW);
            startRecording(false);
        } else if (isRecording) {
            mBuilder.setContentText("Tracking route")
                    // uses default progress bar
                    .setProgress(100, progress, false)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else {
            mBuilder
            // show the route recorder is not tracking
            .setContentText("Not Tracking").setPriority(
                    NotificationCompat.PRIORITY_LOW);
        }

        // link the notifications to the recorder activity
        Intent resultIntent = new Intent(context, KmlReader.class);
        resultIntent
                .setAction(ServiceLocationRecorder.INTENT_COM_GOSYLVESTER_BESTRIDES_LOCATION_RECORDER);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent
                .getActivity(context, 0, resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        return mBuilder.build();
    }
}

@Override
public void onCreate() {
    ServiceLocationRecorder.weakContext = new WeakReference<Context>(
            getApplicationContext());
    locationListener = (LocationListener) this;
    connectionCallbacks = (GooglePlayServicesClient.ConnectionCallbacks) this;
    onConnectionFailedListener = (OnConnectionFailedListener) this;
    // get the notification manager
    // get the db distance
}


private static WeakReference<Context> weakContext = null;

private static LocationClient locationClient;
private static int numberOfLocations = 0;

private static LocationListener locationListener;
private static GooglePlayServicesClient.ConnectionCallbacks connectionCallbacks = null;
private static OnConnectionFailedListener onConnectionFailedListener = null;
boolean isRecording=false;
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
if(intent!=null){
//意图是处理=b
布尔b=intent.getBooleanExtra(setingRecord.RECORDING,
以色列记录);
//用于开始录制的自定义方法,该方法更改isRecording标志。
开始记录(b);
}
如果(isRecording){
startForeground(R.id.action_记录,getCustomNotification());
return Service.START\u STICKY;
}否则{
停止前景(真);
退货服务。开始时不粘;
}
}
私有静态通知getCustomNotification(){
Context=weakContext.get();
if(上下文==null){
返回null;
}否则{
//计算进度。
int progress=numberOfLocations*100/最大位置;
如果(进度>100){
进度=100;
}
字符串跟踪;
if(isMiles){
trackedMsg=“已跟踪”
+十进制格式(距离*米到英里)
+“英里”;
}否则{
trackedMsg=“已跟踪”
+十进制格式(距离*米到公里)
+“公里”;
}
//构建通知。
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(
(上下文)
//通知1
.setContentTitle(“最佳骑乘”)
//通知2
setLargeIcon先生(
BitmapFactory.decodeResource(
context.getResources(),
R.可牵引ic_发射器)
//通知4地点数目
.setContentInfo(trackedMsg)
//通知5
.setSmallIcon(R.drawable.ic_启动器2)
//通知6地点的时间
//用户无法取消录制消息,因为它
//连续地
//更新
.SET正在进行(isRecording)
//自动取消通知,使其显示为服务
//有
//停止
.setAutoCancel(真);
//通知3路线名称
如果(进度==100){
mBuilder.setTicker(“跟踪器已满”)
//显示路线记录器已满
.setContentText(“跟踪器已满”)
.setPriority(通知兼容优先级低);
startRecording(假);
}否则如果(
mLocationClient.removeLocationUpdates(pendingInent);
pendingIntent.cancel();

//Now check whether PendingIntent exists.
Intent locationUpdatesIntent = new Intent(context, LocationUpdatesIntentService.class);
PendingIntent pendingInent = PendingIntent.getService(context, 0, locationUpdatesIntent, PendingIntent.FLAG_NO_CREATE);
boolean isLocationUpdatesEnabled = (pendingIntent != null);