代码之家  ›  专栏  ›  技术社区  ›  V_Coder_Z

接近警报仅触发一次

  •  1
  • V_Coder_Z  · 技术社区  · 10 年前

    我想获取当前位置并向该位置添加接近警报。当我退出或输入当前位置周围的半径时。当我获取当前位置的纬度和经度并添加接近警报时,我一注册就收到警报。但是,即使我在前台,我也无法确定何时退出。我已经在清单上登记了我的收款人。

    这是我的活动。

        public class GeoFencer extends FragmentActivity implements LocationListener {
    
        private LocationManager locMan, locManForProximitySensor;
        private Intent i;
        private PendingIntent pService;
        Context context;
    
    
        @Override
        protected void onCreate(Bundle arg0) {
            setContentView(R.layout.geo_fencer_layout);
            super.onCreate(arg0);
            context = this;
    
            locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locManForProximitySensor = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            i = new Intent("com.packagename.proximity");
            pService = PendingIntent.getBroadcast(this, 0   , i, 0);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // TODO Auto-generated method stub
            MenuInflater inflater= getMenuInflater();
            inflater.inflate(R.menu.geofencemenu, menu);
    
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            if(item.getItemId() == R.id.action_geo){
                if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                }
                else if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                }
                else
                    Toast.makeText(this, "GPS is disabled. Please Enable.", Toast.LENGTH_LONG).show();
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
    
    
            locManForProximitySensor.addProximityAlert(location.getLatitude(), location.getLongitude(), 10, -1 , pService) ;
            Log.d(getClass().getSimpleName(),"Proximity alert added (" + location.getLatitude() + ", "+ location.getLongitude() + ")");
            locMan.removeUpdates(this);
    
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
    }
    

    我的演奏家

       public class ProximityAlertReceiver extends BroadcastReceiver{
        private static final int NOTIFICATION_ID = 1000;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String key = LocationManager.KEY_PROXIMITY_ENTERING;
            String message  = " ";
            Boolean entering = intent.getBooleanExtra(key, false);
    
            if (entering) {
                Log.d(getClass().getSimpleName(), "entering");
                message = "You are entering your point";
            }
            else {
                Log.d(getClass().getSimpleName(), "exiting");
                message = "You are exiting your point";
            }
    
    
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.app_icon)
                    .setContentTitle("CompanyName")
                    .setContentText(message);
    
    
            NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    
        }
    }
    

    请告诉我我在这里做错了什么。谢谢阿达万斯。

    1 回复  |  直到 10 年前
        1
  •  0
  •   simas    10 年前

    您应该添加以下标志之一:PendingIntent.FLAG_CANCEL_CURRENT或PendingIntnt.FLAG_UPDATE_CURREN。