当您走进地理围栏时,如何为简单的信息敬酒?

Posted

技术标签:

【中文标题】当您走进地理围栏时,如何为简单的信息敬酒?【英文标题】:How to toast a simple message when you walk into a Geofence? 【发布时间】:2017-02-02 11:12:38 【问题描述】:

所以我对地理围栏完全陌生,我按照以下教程设置地理围栏:http://io2015codelabs.appspot.com/codelabs/geofences#4。

代码运行了,但是当我走进地理围栏时,什么也没有发生...当我进入或离开地理围栏时,我将如何敬酒?我的代码与指南中的代码完全相同。所以我希望你们能帮助我举一些例子。我

这里是地理围栏类:

public class GeoFence extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        ResultCallback<Status>

    protected ArrayList<Geofence> mGeofenceList;
    protected GoogleApiClient mGoogleApiClient;
    private Button mAddGeofencesButton;

    @Override
    public void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geo_fence);
        mAddGeofencesButton = (Button) findViewById(R.id.add_geofences_button);
        // Empty list for storing geofences.
        mGeofenceList = new ArrayList<Geofence>();

        // Get the geofences used. Geofence data is hard coded in this sample.
        populateGeofenceList();

        // Kick off the request to build GoogleApiClient.
        buildGoogleApiClient();

    

    public void populateGeofenceList() 
        for (Map.Entry<String, LatLng> entry : Constants.LANDMARKS.entrySet()) 
            mGeofenceList.add(new Geofence.Builder()
                    .setRequestId(entry.getKey())
                    .setCircularRegion(
                            entry.getValue().latitude,
                            entry.getValue().longitude,
                            Constants.GEOFENCE_RADIUS_IN_METERS
                    )
                    .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                            Geofence.GEOFENCE_TRANSITION_EXIT)
                    .build());
        
    
    protected synchronized void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    
    public void addGeofencesButtonHandler(View view) 
        if (!mGoogleApiClient.isConnected()) 
            Toast.makeText(this, "Google API Client not connected!", Toast.LENGTH_SHORT).show();
            return;
        

        try 
            LocationServices.GeofencingApi.addGeofences(
                    mGoogleApiClient,
                    getGeofencingRequest(),
                    getGeofencePendingIntent()
            ).setResultCallback(this); // Result processed in onResult().
         catch (SecurityException securityException) 
            // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        
    
    private GeofencingRequest getGeofencingRequest() 
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(mGeofenceList);
        return builder.build();
    
    private PendingIntent getGeofencePendingIntent() 
        Intent intent = new Intent(this, GTIS.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling addgeoFences()
        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    @Override
    public void onConnected(Bundle connectionHint) 

    

    @Override
    public void onConnectionFailed(ConnectionResult result) 
        // Do something with result.getErrorCode());
    
    @Override
    public void onConnectionSuspended(int cause) 
        mGoogleApiClient.connect();
    
    @Override
    protected void onStart() 
        super.onStart();
        if (!mGoogleApiClient.isConnecting() || !mGoogleApiClient.isConnected()) 
            mGoogleApiClient.connect();
        
    

    @Override
    protected void onStop() 
        super.onStop();
        if (mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected()) 
            mGoogleApiClient.disconnect();
        
    

    public void onResult(Status status)
    
        if (status.isSuccess()) 
            Toast.makeText(
                    this,
                    "Geofences Added",
                    Toast.LENGTH_SHORT
            ).show();
         else 
           Toast.makeText(this,"No geo fence :(",Toast.LENGTH_SHORT).show();
        
    

这是 GeofenceTransitionsIntentService 类

public class GTIS extends IntentService

    protected static final String TAG = "GeofenceTransitionsIS";

    public GTIS() 
        super(TAG);  // use TAG to name the IntentService worker thread
    

    @Override
    protected void onHandleIntent(Intent intent) 
        GeofencingEvent event = GeofencingEvent.fromIntent(intent);
        if (event.hasError()) 
            Log.e(TAG, "GeofencingEvent Error: " + event.getErrorCode());
            String description = getGeofenceTransitionDetails(event);
            sendNotification(description);
            return;
        
    
    private static String getGeofenceTransitionDetails(GeofencingEvent event) 
        String transitionString =
                GeofenceStatusCodes.getStatusCodeString(event.getGeofenceTransition());
        List triggeringIDs = new ArrayList();
        for (Geofence geofence : event.getTriggeringGeofences()) 
            triggeringIDs.add(geofence.getRequestId());
        
        return String.format("%s: %s", transitionString, TextUtils.join(", ", triggeringIDs));
    

    private void sendNotification(String notificationDetails) 
        // Create an explicit content Intent that starts MainActivity.
        Intent notificationIntent = new Intent(getApplicationContext(), Geofence.class);

        // Get a PendingIntent containing the entire back stack.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(GeoFence.class).addNextIntent(notificationIntent);
        PendingIntent notificationPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get a notification builder that's compatible with platform versions >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        // Define the notification settings.
        builder.setColor(Color.RED)
                .setContentTitle(notificationDetails)
                .setContentText("Click notification to return to App")
                .setContentIntent(notificationPendingIntent)
                .setAutoCancel(true);

        // Fire and notify the built Notification.
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    


这是我的常量类:

public class Constants

    public static final long GEOFENCE_EXPIRATION_IN_MILLISECONDS = 12 * 60 * 60 * 1000;
    public static final float GEOFENCE_RADIUS_IN_METERS = 20;

    public static final HashMap<String, LatLng> LANDMARKS = new HashMap<String, LatLng>();
    static 
        // San Francisco International Airport.
        LANDMARKS.put("Westwood", new LatLng(34.067313, -118.461164)); //34.065767, -118.459658  LANDMARKS.put("Westwood", new LatLng(34.065767,  -118.459658));

        // Googleplex.
        LANDMARKS.put("Japantown", new LatLng(37.785281,-122.4296384));

        // Test
        LANDMARKS.put("SFO", new LatLng(37.621313,-122.378955));
    

【问题讨论】:

按照下一步,在onHandleIntent某处显示吐司。 @stkent 所以我只是在 onHandleIntent 中写了一个简单的 Toast 语句? 我的意思是我按照指南的方式实现了 onHandleIntent,但什么也没发生...? 【参考方案1】:

指南中的代码显示Notification 而不是Toast。它对您不起作用,因为您在 if(event.hasError()) 语句中调用 sendNotification(),在您的情况下不会触发,因为没有发生错误

你需要做的是:

@Override
protected void onHandleIntent(Intent intent) 
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event.hasError()) 
        Log.e(TAG, "GeofencingEvent Error: " + event.getErrorCode());
    
    String description = getGeofenceTransitionDetails(event);
    sendNotification(description);

同样,此代码将显示 Notification,这比在现实应用中的 Toast 更有意义,原因如下:

    在地理围栏转换时,您的应用甚至可能未处于活动状态,并且突然出现的 Toast 对用户来说可能看起来很奇怪。 通知允许您显示更多信息(标题和文本),并可能直接返回您的应用程序 它让用户有机会在方便时阅读和关闭它

如果你仍然想显示Toast,你应该像在Activity中那样做(IntentService也是Context):

Toast.makeText(this, description, Toast.LENGTH_LONG).show(); 

【讨论】:

以上是关于当您走进地理围栏时,如何为简单的信息敬酒?的主要内容,如果未能解决你的问题,请参考以下文章

Swift:用户位置附近的地理围栏/地理位置

使用半径内的设备添加地理围栏时从未收到地理围栏停留事件

Android:制作地理围栏的简单方法?

Android如何知道地理围栏何时过期?

地理围栏:如何识别对象(特征),使用 Oracle Spatial 重叠地理围栏边界?

什么是 “地理围栏”?