地理围栏样本接收器问题

时间:2014-01-18 09:33:59

标签: android android-intent

我正在开发一个使用地理围栏的应用程序,除了一件事以外,一切正常:

来自主类的我的广播接收器内部类没有得到那些通知地理围栏转换的意图。但是,我从ReceiveTransitionsIntentService收到了我的通知提醒 当我转移地理围栏但我也想在Main活动中设置位置更改侦听器。 好的,我要展示一些代码:

这是ReceiveTransitionsIntentService(效果很好):

 public class ReceiveTransitionsIntentService extends IntentService  {

 public ReceiveTransitionsIntentService() {
 super("ReceiveTransitionsIntentService");
    }


protected void onHandleIntent(Intent intent) {

    Intent broadcastIntent = new Intent();

    broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

    if (LocationClient.hasError(intent)) {

        int errorCode = LocationClient.getErrorCode(intent);

        String errorMessage = LocationServiceErrorMessages.getErrorString(
                this, errorCode);

        Log.e(GeofenceUtils.APPTAG,
                getString(R.string.geofence_transition_error_detail,
                        errorMessage));

        broadcastIntent
                .setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);

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

        // notification
    } else {

        int transition = LocationClient.getGeofenceTransition(intent);

        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
                || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

            List<Geofence> geofences = LocationClient
                    .getTriggeringGeofences(intent);
            String[] geofenceIds = new String[geofences.size()];
            for (int index = 0; index < geofences.size(); index++) {
                geofenceIds[index] = geofences.get(index).getRequestId();
            }
            String ids = TextUtils.join(
                    GeofenceUtils.GEOFENCE_ID_DELIMITER, geofenceIds);
            String transitionType = getTransitionString(transition);

            sendNotification(transitionType, ids);

            Log.d(GeofenceUtils.APPTAG,
                    getString(
                            R.string.geofence_transition_notification_title,
                            transitionType, ids));
            Log.d(GeofenceUtils.APPTAG,
                    getString(R.string.geofence_transition_notification_text));

        } else {
            // Always log as an error
            Log.e(GeofenceUtils.APPTAG,
                    getString(R.string.geofence_transition_invalid_type,
                            transition));
        }
    }
}


private void sendNotification(String transitionType, String ids) {
    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_ALARM);

    Intent notificationIntent =
            new Intent(getApplicationContext(),GeofenceMain.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    stackBuilder.addParentStack(GeofenceMain.class);

    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);


            builder.setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                   .setSound(soundUri)
                   .setContentText(" This is a notification" )
                   .setContentIntent(notificationPendingIntent);


            NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            mNotificationManager.notify(0, builder.build());
          }

private String getTransitionString(int transitionType) {
    switch (transitionType) {

    case Geofence.GEOFENCE_TRANSITION_ENTER:
        return getString(R.string.geofence_transition_entered);

    case Geofence.GEOFENCE_TRANSITION_EXIT:
        return getString(R.string.geofence_transition_exited);

    default:
        return getString(R.string.geofence_transition_unknown);
    }
}   
}

这是我主要活动中的一段代码,我没有得到有关转换发生的任何信息。

   public class GeofenceMain extends FragmentActivity 
     {
           ...

      public class GeofenceSampleReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        String action = intent.getAction();

        if (TextUtils.equals(action, GeofenceUtils.ACTION_GEOFENCE_ERROR)) {

            handleGeofenceError(context, intent);

        } else if (TextUtils.equals(action,
                GeofenceUtils.ACTION_GEOFENCES_ADDED)
                || TextUtils.equals(action,
                        GeofenceUtils.ACTION_GEOFENCES_REMOVED)) {

            handleGeofenceStatus(context, intent);

        } else if (TextUtils.equals(action,
                GeofenceUtils.ACTION_GEOFENCE_TRANSITION)) {

            handleGeofenceTransition(context, intent);

        } else {
            Log.e(GeofenceUtils.APPTAG,
                    getString(R.string.invalid_action_detail, action));
            Toast.makeText(context, R.string.invalid_action,
                    Toast.LENGTH_LONG).show();
        }
    }


    private void handleGeofenceStatus(Context context, Intent intent) {
        //THIS ONE IS WORKING
    }


    private void handleGeofenceTransition(Context context, Intent intent) {
        //OVER HERE IS THE PROBLEM!
    }


    private void handleGeofenceError(Context context, Intent intent) {
        String msg = intent
                .getStringExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS);
        Log.e(GeofenceUtils.APPTAG, msg);
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
} }

最后的话:我需要一些帮助来检测为什么我没有得到包含GeofenceUtils.ACTION_GEOFENCE_TRANSITION动作的意图。

0 个答案:

没有答案