代码之家  ›  专栏  ›  技术社区  ›  user7060819

onHandleWork从不从挂起的意向地理围栏接收意向

  •  2
  • user7060819  · 技术社区  · 7 年前

    我正在做一个 Geofencing 应用这个 JobIntentService 处理 GeofenceTransitions 从不接受意图。我每隔一分钟接收一次位置更新,然后创建一个新的地理围栏列表,然后根据用户的当前位置添加地理围栏。

    这是我的 AndroidManifest.xml

     ........
    
     <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <service
                android:name=".GeofenceTransitionsService"
                android:exported="true"
                android:permission="android.permission.BIND_JOB_SERVICE">
    
            </service>
    
            <receiver
                android:name=".GeofenceBroadcastReceiver"
                android:enabled="true"
                android:exported="true" />
    .............
    

    我的 GeofenceBroadcastReceiver.java

    public class GeofenceBroadcastReceiver extends BroadcastReceiver {
    
        /**
         * Receives incoming intents.
         *
         * @param context the application context.
         * @param intent  sent by Location Services. This Intent is provided to Location
         *                Services (inside a PendingIntent) when addGeofences() is called.
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            // Enqueues a JobIntentService passing the context and intent as parameters
            GeofenceTransitionsService.enqueueWork(context, intent);
        }
    }
    

    我的 GeofenceTransitionsService.java 处理触发的地理围栏

    public class GeofenceTransitionsService extends JobIntentService {
       ..........
    
        /**
         * Convenience method for enqueuing work in to this service
         * Enqueue new work to be dispatched to onHandleWork
         */
        public static void enqueueWork(Context context, Intent intent) {
            Log.d(TAG, "Received intent: " + intent);
            enqueueWork(context, GeofenceTransitionsService.class, JOB_ID, intent);
        }
    
    @Override
        protected void onHandleWork(Intent intent){
            // We have received work to do.  The system or framework is already
            // holding a wake lock for us at this point, so we can just go.
            Log.d(TAG, "Received intent: " + intent);
        }
    }
    

    这是我的部分代码 PointOfInterestMapFragment.java 创建地理围栏请求,创建挂起的意图并添加地理围栏

         /* Use the GeofencingRequest class and its nested GeofencingRequestBuilder
             * class to specify the geofences to monitor and to set how related geofence events are
             * triggered
             */
            private GeofencingRequest getGeofencingRequest(){
                GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
                //tell Location services that GEOFENCE_TRANSITION_DWELL should be triggered if the
                //device is already inside the geofence
                builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
                builder.addGeofences(mGeofenceList);
                return builder.build();
            }//end method getGeofencingRequest
    
            /*Pending intent that starts the IntentService*/
            private PendingIntent getGeofencePendingIntent(){
                Log.d(TAG, "getPendingIntent()");
                //Reuse the pending intent if we already have it
                if(mGeofencePendingIntent != null) {
                    return mGeofencePendingIntent;
                }
    
                Intent intent = new Intent(getActivity(), GeofenceBroadcastReceiver.class);
    
                // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
                // calling addGeofences() and removeGeofences().
                mGeofencePendingIntent = PendingIntent.getBroadcast(getActivity()
                        , 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                return mGeofencePendingIntent;
            }//end method PendingIntent
    
            /*Add geofences*/
            @SuppressWarnings("MissingPermission")
            private void addGeofence(){
                if(checkPermissions()){
                    mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
    
                                    Log.d(TAG, "Geofence added");
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.d(TAG, "Failed to add geofence: " + e.getMessage());
                                }
                            })
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //drawGeofence();
    
                                }
                            });
    
    
                }else{
                    requestPermissions();
                }
            }//end method addGeofence
    

    下面是中的代码部分 PointOfInterestMapFragment。Java语言 我在哪里接收位置更新,填充 GeofenceList 然后添加地理围栏

    /**
         * Creates a callback for receiving location events.
         */
        private void createLocationCallback() {
            mLocationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);
    
                    mCurrentLocation = locationResult.getLastLocation();
                    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    
                    //populateGeofenceList to reflect the new current location bounds
                    populateGeofenceList();
                    addGeofence();
    
                }
            };
        }
    

    当应用程序执行时,我从代码行中获得log cat中的消息 Log.d(TAG, "getPendingIntent()"); 在里面 getGeofencePendingIntent() 但永远不要获取应该显示在中的消息 onHandleWork() 方法

    1 回复  |  直到 7 年前
        1
  •  0
  •   theTypan    7 年前

    我也有类似的“问题”。代码很好。在我的例子中,我认为代码不起作用,因为我没有正确理解地理围栏的工作原理。我想加上地理围栏,对你来说 addGeofence() 是触发器,所以我一直在等待在特定时间点看到通知。然而,对该方法的调用只会为 监测 ,则仅当满足任何筛选器时,才会将意图传递给服务( Geofence.GEOFENCE_TRANSITION_DWELL , Geofence.GEOFENCE_TRANSITION_EXIT Geofence.GEOFENCE_TRANSITION_ENTER )在添加的地理围栏上。您可以从文档中阅读更多内容 here

    因此,您可能会收到 添加了Geofence 日志猫中的消息,但这就是它的字面意思,地理围栏已添加,未触发。添加地理围栏后等待一段时间,如果任何过滤器对添加的地理围栏满意,则发送意向。所以对我有效的解决方案是 等待 一段时间后,我收到了意向和通知。

    如果等待不起作用,您可能需要扩展 GEOFENCE_RADIUS ,对3000米进行检查,看是否有任何变化。此外,将过期持续时间设置为更大的值或 Geofence.NEVER_EXPIRE