簡體   English   中英

接近警報僅觸發一次警報

[英]Proximity alert firing alert only once

我想獲取當前位置,並向該位置添加一個接近警報。 每當我退出或輸入當前位置周圍的半徑時。 在獲取當前位置的緯度和經度並添加鄰近警報時,我在注冊后會立即收到警報。 但是,即使我在前台,退出時也沒有得到。 我已經在清單上登記了收貨人。

這是我的活動。

    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());

    }
}

請告訴我我在做什么錯。 感謝Adavance。

您應該添加以下標志之一:PendingIntent.FLAG_CANCEL_CURRENT或PendingIntent.FLAG_UPDATE_CURRENT。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM