android - 地理围栏转换后,我如何在主要 Activity 上做些什么?

标签 android google-maps-android-api-2 android-geofence

当有人进入我的地理围栏之一时,我想在我的 map 上显示一个布局并开始播放一些音频。 我一直在学习一些教程,我知道如何在它们进入时进行推送通知,但这一切都发生在另一个 IntentService 类中。

也许这就是 onResult 的用途?我不确定如何从结果参数中获取地理围栏信息

LocationServices.GeofencingApi.addGeofences(
            gApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this);

 @Override
public void onResult(Result result) {


}

这是我的 intentService 类

public class GeofenceTransitionsIntentService extends IntentService {
private final String TAG = "Geofence_transitions";


public GeofenceTransitionsIntentService() {
    super("geo-service");
}


@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = geofencingEvent.getErrorCode()+"";
        Log.e(TAG, errorMessage);
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger
        // multiple geofences.
        ArrayList<Geofence> triggeringGeofences = (ArrayList<Geofence>) geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
                this,
                geofenceTransition,
                triggeringGeofences
        );

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_media_play)
                        .setContentTitle("Geofence!")
                        .setContentText(geofenceTransitionDetails);

        mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(geofenceTransitionDetails).setBigContentTitle("Big title")
                .setSummaryText(geofenceTransitionDetails));


        // Send notification and log the transition details.
        int mNotificationId = 001;

        NotificationManager mNotifyMgr =
                (NotificationManager)    getSystemService(NOTIFICATION_SERVICE);

        mNotifyMgr.notify(mNotificationId, mBuilder.build());

    } else {
        // Log the error.
        Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
                geofenceTransition));

    }

}

人们通常如何获取触发地理围栏的信息并在主线程上执行某些操作?

最佳答案

使用LocalBroadcastManager向任何必须收听事件的上下文发送广播。以下是您应该在 Activity 中执行的操作的示例:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceBundle);
        //Your code here

        LocalBroadcastManager lbc = LocalBroadcastManager.getInstance(this);
        GoogleReceiver googleReceiver = new GoogleReceiver(this);
        lbc.registerReceiver(googleReceiver, new IntentFilter("googlegeofence"));
        //Anything with this intent will be sent to this receiver
    
    }

    static class GoogleReceiver extends BroadcastReceiver{

        MyActivity mActivity;

        public GoogleReceiver(Activity activity){
            mActivity = (MyActivity)activity;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            //Handle the intent here
        }
    }
}

这是您应该为您的 intentservice 写下的内容:

public class GeofenceTransitionsIntentService extends IntentService {

//Any stuff you need to do here    

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

    //Collect all the stuff you want to send to activity here


    Intent lbcIntent = new Intent("googlegeofence"); //Send to any reciever listening for this

    lbcIntent.putExtra("Key", Value);  //Put whatever it is you want the activity to handle

    LocalBroadcastManager.getInstance(this).sendBroadcast(lbcIntent);  //Send the intent
}

关于android - 地理围栏转换后,我如何在主要 Activity 上做些什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384101/

相关文章:

android - 用gson反序列化json

android - 在 Google Maps Api v2 中添加多个标记

android - Google Maps API v2 Android 添加可绘制形状作为标记

应用程序被杀死时的 Android 地理围栏

android - Geofence Android 无法正常工作(未调用 IntentService)

android - 位置服务 onProviderEnabled 从未调用过

java - 如何按自定义 ArrayAdapter 所基于的对象的变量对它的内容进行排序?

android - 华为广告工具包横幅广告返回错误代码1

android - 谷歌地图删除标记路线上下文菜单

Android:如何让用户为其他跟踪用户创建地理围栏?