2

アプリでジオフェンスを使用しようとすると問題が発生します。ジオフェンスの登録には成功しましたが、通知が届きません。

最初に、http://developer.android.com/training/location/geofencing.htmlで提供されている例に従いますが、エリアに向かって歩いていても、エリアから離れていても、コードがインテント サービスをトリガーすることはありません。

ジオフェンスを登録し、インテント サービスを呼び出すための私のコードは次のとおりです。

内部マニフェスト:

<service
        android:name="com.example.zukami.apps.blynk.geofence.ReceiveTransitionsIntentService"
        android:exported="true" >
    </service>

私のMainActivityで

    private boolean registerGeofence() {
        mRequestType = GeofenceUtils.REQUEST_TYPE.ADD;
        if (!servicesConnected()) {
            return false;
        }


        SimpleGeofence testGeo = new SimpleGeofence(Flag.GEOFENCE + "_"
                + 1, Double.valueOf("1.317342"),
                Double.valueOf("103.841998"), (float) (200),
                GEOFENCE_EXPIRATION_IN_HOURS,
                Geofence.GEOFENCE_TRANSITION_ENTER);

        mPrefs.setGeofence(Flag.GEOFENCE + "_" + 1, testGeo);
        mCurrentGeofences.add(testGeo.toGeofence());

        SimpleGeofence testGeo2 = new SimpleGeofence(Flag.GEOFENCE + "_"
                + 2, Double.valueOf("1.303961"),
                Double.valueOf("103.909356"), (float) (200),
                GEOFENCE_EXPIRATION_IN_HOURS,
                Geofence.GEOFENCE_TRANSITION_ENTER);

        mPrefs.setGeofence(Flag.GEOFENCE + "_" + 2, testGeo2);
        mCurrentGeofences.add(testGeo2.toGeofence());
        // end deleted this code after testing

        try {
            mGeofenceRequester.addGeofences(mCurrentGeofences);
            Log.e(TAG, "ADDING GEOFENCE ??? YES WE DID IT");
        } catch (UnsupportedOperationException e) {
            Toast.makeText(this,
                    R.string.add_geofences_already_requested_error,
                    Toast.LENGTH_LONG).show();
        }
        return true;
    }

 @Override
    protected void onResume() {
        super.onResume();
        // Register the broadcast receiver to receive status updates
        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter);

私のジオフェンスリクエスターで

private PendingIntent createRequestPendingIntent() {
     Log.e(TAG, "CREATE REQUEST PENDING INTENT");
     // If the PendingIntent already exists
     if (null != mGeofencePendingIntent) {
     Log.e(TAG, "PENDING INTENT NOT NULL");
     return mGeofencePendingIntent;

     // If no PendingIntent exists
     } else {
     Log.e(TAG, "PENDING INTENT  NULL, LET'S CREATED IT");
     // Create an Intent pointing to the IntentService
     Intent intent = new Intent(mActivity,
     ReceiveTransitionsIntentService.class);
     Log.e(TAG,
     return PendingIntent.getService(mActivity, 0, intent,
     PendingIntent.FLAG_UPDATE_CURRENT);
     }

IntentService のサブクラスであるクラスでは、例と同じままです。

私はこの問題を検索し、このリンクで説明されているこの解決策を見つけました: Android Geofenceは最終的に遷移インテントの取得を停止するため、提案に従ってコードを正確に変更しますが、それでもGeofenceReceiverクラス内のコードから通知を取得できません。この行のコード

LocationClient.getGeofenceTransition(インテント);

常に -1 を返します。これは、エリアに出入りしないことを意味するため (GEOFENCE NEVER_EXPIRED を示します)、通知を受け取ることはありません。私の GeofenceReceiver クラスの下に見つけてください:

public class GeofenceReceiver extends BroadcastReceiver {

    public static String TAG = GeofenceReceiver.class.getCanonicalName();
    Context context;
    Intent broadcastIntent = new Intent();

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

        if (LocationClient.hasError(intent)) {
            handleError(intent);
        } else {
            handleEnterExit(intent);
        }
    }

    private void handleError(Intent intent) {
        int errorCode = LocationClient.getErrorCode(intent);
        String errorMessage = LocationServiceErrorMessages.getErrorString(
                context, errorCode);
        Log.e(GeofenceUtils.APPTAG, context.getString(
                R.string.geofence_transition_error_detail, errorMessage));

        broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);
        LocalBroadcastManager.getInstance(context).sendBroadcast(
                broadcastIntent);
    }

    private void handleEnterExit(Intent intent) {

        int transition = LocationClient.getGeofenceTransition(intent);

        // Test that a valid transition was reported
        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
                || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {



            // Post a notification
            List<Geofence> geofences = LocationClient
                    .getTriggeringGeofences(intent);
            String[] geofenceIds = new String[geofences.size()];
            String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
                    geofenceIds);
            String transitionType = getTransitionString(transition);

            for (int index = 0; index < geofences.size(); index++) {
                Geofence geofence = geofences.get(index);
                geofenceIds[index] = geofences.get(index).getRequestId();
            }

            sendNotification(transitionType, ids);

            // Create an Intent to broadcast to the app
            broadcastIntent
                    .setAction(GeofenceUtils.ACTION_GEOFENCE_TRANSITION)
                    .addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
                    .putExtra(GeofenceUtils.EXTRA_GEOFENCE_ID, geofenceIds)
                    .putExtra(GeofenceUtils.EXTRA_GEOFENCE_TRANSITION_TYPE,
                            transitionType);

            LocalBroadcastManager.getInstance(context).sendBroadcast(
                    broadcastIntent);

            // Log the transition type and a message
            Log.e(GeofenceUtils.APPTAG, transitionType + ": " + ids);
            Log.e(GeofenceUtils.APPTAG, context
                    .getString(R.string.geofence_transition_notification_text));
            Log.e(GeofenceUtils.APPTAG, "transition");
        } else {
            // Always log as an error
            Log.e(TAG, "TRANSITION = " + transition);
        }
    }

このコードを修正するために何をすべきかアドバイスしてください。親切な助けに本当に感謝しています。

4

3 に答える 3