4

GeoFencing在 Android 上工作,我被困在某一点上。我的任务是在notification用户进入/退出Geofence我定义的区域时向他展示。

这是我的代码:

Activity class

public class TestMapActivity extends FragmentActivity implements    
    OnMarkerDragListener,ConnectionCallbacks, OnConnectionFailedListener,OnAddGeofencesResultListener {

private static GoogleMap map;
private LocationClient mLocationClient;
private PendingIntent mGeofencePendingIntent;
private SimpleGeoFence fence;
private List<Geofence> mGeoList;
private LocationRequest localRequest;
private GeofenceReceiver mBroadcastReceiver;
private IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle saveInstance)
{
        super.onCreate(saveInstance);
        setContentView(R.layout.activity_map);
        map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setOnMarkerDragListener(this);
        CameraPosition INIT =
        new CameraPosition.Builder()
        .target(new LatLng(19.0222, 72.8666))
        .zoom(17.5F)
        .bearing(300F) // orientation
        .tilt( 50F) // viewing angle
        .build();
         map.moveCamera( CameraUpdateFactory.newCameraPosition(INIT) );

 }


   @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return true;
    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
            case R.id.add_fence:

            Toast.makeText(TestMapActivity.this, "Add fence is Selected", Toast.LENGTH_LONG).show();

            fence= new SimpleGeoFence();
            fence.toGeofence();
            addMarkerForFence(fence);
            addIntentForFence();
            return true;
            default:
                 return super.onOptionsItemSelected(item);
        }
    }

     public void addMarkerForFence(SimpleGeoFence fence){
     if(fence == null){
         // display an error message and return
        return;
     }


     //Instantiates a new CircleOptions object +  center/radius
     CircleOptions circleOptions = new CircleOptions()
       .center( new LatLng(19.0216, 72.8646 ))
       .radius( 500 )
       .fillColor(0x40ff0000)
       .strokeColor(Color.TRANSPARENT)
       .strokeWidth(2);

     map.addCircle(circleOptions);

     map.addMarker( new MarkerOptions()
       .position( new LatLng(19.0216, 72.8646) )
       .title("Fence " +fence.getId())
       .snippet("Radius: " +fence.getRadius()) ).showInfoWindow();

     // Get back the mutable Circle
     Circle circle = map.addCircle(circleOptions);

 }

     public void addIntentForFence()
 {

     Geofence geoFence= fence.toGeofence();
     mGeoList = new ArrayList<Geofence>();
     mGeoList.add(geoFence);

     mLocationClient = new LocationClient(this, this, this);
     mLocationClient.connect();

    }

   @Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

    mGeofencePendingIntent = createRequestPendingIntent(); 

    localRequest = LocationRequest.create();
    localRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    localRequest.setInterval(5000);

    mLocationClient.addGeofences(mGeoList, mGeofencePendingIntent , this);  
}

    private PendingIntent createRequestPendingIntent() {

    if (null != mGeofencePendingIntent) {

        return mGeofencePendingIntent;

    } else {


        Intent intent = new Intent("com.example.ACTION_RECEIVE_GEOFENCE");
        System.out.println("Intent" +intent);
        //sendBroadcast(intent);
        return PendingIntent.getBroadcast(
                getApplicationContext(),
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
}
} //end oncreate

GeofenceReceiver class

  public class GeofenceReceiver extends BroadcastReceiver
  {
         public Context context;
         Intent broadcastIntent = new Intent();

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

           // TODO Auto-generated method stub

             this.context = context;
              broadcastIntent.addCategory("com.example.CATEGORY_LOCATION_SERVICES");        
              String action= intent.getAction();

               if (LocationClient.hasError(intent)) {
                       //do something
                } 
               else 
                {
               handleEnterExit(intent);
                 }
           }

         private void handleEnterExit(Intent intent) {

             int transition = LocationClient.getGeofenceTransition(intent);

              System.out.println("transition" +transition); //getting -1 over here
              if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
                || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

                         //send notification over here
             }   

    } 

AndroidManifest.xml容器接收器

    <receiver android:name="com.example.GeofenceReceiver" android:exported="false">
    <intent-filter >
        <action android:name="com.example.ACTION_RECEIVE_GEOFENCE"/>
    </intent-filter>
</receiver>

所以基本上我可以看到我的 GeoFence 被创建,但我没有收到同样的通知。

有人对此有解决方案吗?

4

3 回答 3

3

只需检查它的示例代码。您正在使用地理围栏,但您没有提及位置客户端和位置连接,因此 google play 服务无法连接到客户端,并且您无法接收通知获取 google 请求者文件并检查地理围栏添加到列表中的主要活动并连接位置客户端希望它对你有用

于 2014-06-29T16:27:25.987 回答
1

因此,根据您所说的评论,您可以添加地理围栏,但您没有收到通知。首先,您似乎使用了他们网站上的 android 地理围栏示例代码,并且您已对其进行了更改,因此它使用接收器而不是服务。对我来说,我做了同样的事情,这似乎不是正确的解决方案,我的答案是:

1.) 遵循 google'e 地理围栏中的相同步骤/代码:http: //developer.android.com/training/location/geofencing.html

2.)由于您将服务更改为接收器,因此在您的createRequestPendingIntent()方法中您是否返回了正确的广播意图?我的意思是你有这条线,但它到达那里了吗?那里有东西退还吗?

return PendingIntent.getBroadcast( mActivity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);

3.) 你LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter);的 onResume() 中有一个吗?

我会再次关注谷歌的示例项目,并确保您将服务更改为正确的接收器部分,您应该没问题。

于 2013-11-11T21:31:37.237 回答
0

我有同样的问题。我认为这是因为您正在将应用程序上下文用于待处理的意图,请尝试使用活动。

于 2013-11-05T07:18:35.317 回答