2015-11-06 71 views
-3

我正在创建一个应用程序来添加和删除接近警报。 接近警报工作正常。当我靠近坐标时它会通知我。但是当我不需要他们时,我无法删除它们。这里是我用来添加和删除proximity alerts的代码。接近警报没有删除

当我停下来阅读它们时,警报数比前一次增加了一个。

添加接近警报

private void add_lert() { 
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); 
    String proximitys="com.realtech.proxtest"; 
    Intent i=new Intent(proximitys); 
    pi = PendingIntent.getBroadcast(MainActivity.this, 11, i, PendingIntent.FLAG_CANCEL_CURRENT); 
    locationManager.addProximityAlert(lat, lng, radius, -1, pi); 
    IntentFilter filter=new IntentFilter(proximitys); 
    registerReceiver(new ProxReceiver(),filter); 
} 

删除警报

private void remove_alert() { 
    LocationManager locationManager1=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    String proximitys="com.realtech.proxtest"; 
    Intent i=new Intent(proximitys); 
    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 11, i, PendingIntent.FLAG_CANCEL_CURRENT); 
    locationManager1.removeProximityAlert(pi); 
} 

回答

0

您是否尝试过使用全局变量的PendingIntent而不是声明每个块中一个新的(添加/删除)。

另外locationManager在两个块中有不同的名称。尽量为它使用全局变量。

+0

尝试过没有运气的全局变量。 :( –