2013-05-09 67 views
2

我想要做的是有一个接近警报服务,只有当您步入半径(不停止服务)时才会触发通知。我的代码会在每次进入半径内以及每次走出半径时触发通知。我一直在尝试与布尔和removeProximityAlert,但没有成功。有任何想法吗?Android - 通知后删除感应警报

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.location.LocationManager; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class ProximityService extends Service { 

    private String PROX_ALERT_INTENT = "com.example.proximityalert"; 
    private BroadcastReceiver locationReminderReceiver; 
    private LocationManager locationManager; 
    private PendingIntent proximityIntent; 

@override 
    public void onCreate() { 
     locationReminderReceiver = new ProximityIntentReceiver(); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     double lat = 55.586568; 
     double lng = 13.0459; 
     float radius = 1000; 
     long expiration = -1; 

     IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
     registerReceiver(locationReminderReceiver, filter); 

     Intent intent = new Intent(PROX_ALERT_INTENT); 

     intent.putExtra("alert", "Test Zone"); 

     proximityIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     locationManager.addProximityAlert(lat, lng, radius, expiration, proximityIntent); 

    } 

@override 
    public void onDestroy() { 
     Toast.makeText(this, "Proximity Service Stopped", Toast.LENGTH_LONG).show(); 
     try { 
      unregisterReceiver(locationReminderReceiver); 
     } catch (IllegalArgumentException e) { 
      Log.d("receiver", e.toString()); 
     } 

    } 

@override 
    public void onStart(Intent intent, int startid) { 
     Toast.makeText(this, "Proximity Service Started", Toast.LENGTH_LONG).show(); 
    } 

@override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 



    public class ProximityIntentReceiver extends BroadcastReceiver { 

     private static final int NOTIFICATION_ID = 1000; 

    @suppressWarnings("deprecation") 
    @override 
     public void onReceive(Context arg0, Intent arg1) { 

      String place = arg1.getExtras().getString("alert"); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0, arg1, 0); 

      Notification notification = createNotification(); 

      notification.setLatestEventInfo(arg0, "Entering Proximity!", "You are approaching a " + place + " marker.", pendingIntent); 

      notificationManager.notify(NOTIFICATION_ID, notification); 

      locationManager.removeProximityAlert(proximityIntent); 

     } 

     private Notification createNotification() { 
      Notification notification = new Notification(); 

      notification.icon = R.drawable.ic_launcher; 
      notification.when = System.currentTimeMillis(); 

      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notification.defaults |= Notification.DEFAULT_SOUND; 

      return notification; 
     } 

    } 
} 
+0

做尝试也许因为它通过它已经通过不同的背景下,现在 – JRowan 2013-05-09 21:04:58

+0

是啊,试过太 – KKO 2013-05-09 21:15:09

+0

我读使用String键= LocationManager.KEY_PROXIMITY_ENTERING移除,而不是proximityIntent的PendingIntent; \t \t \t \t \t \t布尔输入= arg1.getBooleanExtra(key,false);在onRecieve和使用if(进入){}其他{}是进入和退出,我不能让它工作,虽然 – JRowan 2013-05-09 21:18:06

回答

0

它触发后,您应该删除接近警报,而不是再重新创建它(节省一些标志,作为一个变量,或者,如果你使用SQLite,在你的分贝)。

删除警报是有点棘手,有2层要采取的步骤,都将产生不想要的结果(显然)“开火”:

  1. 注销您的接收器

    您可以保存接收器(或接收器的阵列)和直接unregister它:

    for (ProximityReceiver proximityReceiverLocal:proximityReceiverArray) { 
        context.unregisterReceiver(proximityReceiverLocal); 
        proximityReceiverArray.remove(proximityReceiverLocal); // clear pile 
    } 
    
  2. 删除警报

    注意:您无法将警报另存为对象,您必须重新创建PendingIntent并将其提交至removeProximityAlert方法。挂起的意图必须具有相同的意图(即相同action name)和相同的未决意图ID:

    public void removeProximityAlert(int pendingIntentIdIn, String intentActionNameIn) { 
        Intent intent = new Intent(intentActionNameIn); 
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context , pendingIntentIdIn, intent, 0); 
        locationManager.removeProximityAlert(pendingIntent); 
    } 
    

如果你删除一个,而不是其他,你会达到什么您的预期目标,你什么时候出现的输入POI的半径,但是会浪费宝贵的电池寿命和内存。