2011-07-04 20 views
0

我已經爲2個不同的點指定了2個接近度警報。 對於每個接近警報,我已經指定了一個eventID,如下面的代碼所示。對於每個接近警報(對於每個點),我想要不同的通知聲音。主要問題是我如何在同一個班級內創建不同的通知(通知聲音)。我可以在同一類擴展廣播接收器中指定不同的通知嗎?

這是我使用的代碼。

private void setProximityAlert(double lat, double lon, final int eventID,int requestCode) { 

      float radius = 10f;   

      long expiration =-1; 
      LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      Bundle extras = new Bundle();      
      Intent intent = new Intent(PROXIMITY_INTENT_ACTION);     
      intent.putExtra(ProximityAlert.EVENT_ID_INTENT_EXTRA, eventID); 
      intent.putExtra(PROXIMITY_INTENT_ACTION, extras); 
      PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT); 

       locManager.addProximityAlert(
        lat, 
        lon, 
        radius, 
        expiration, 
        proximityIntent 
       );  
      } 

private void initialiseReceiver() 
     { 
      IntentFilter filter = new IntentFilter(PROXIMITY_INTENT_ACTION); 
      registerReceiver(new ProximityAlert(), filter); 
     } 

在廣播接收器類別i具有在下面的代碼

public class ProximityAlert extends BroadcastReceiver { 

      public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey"; 




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

       int eventID = intent.getIntExtra(EVENT_ID_INTENT_EXTRA, 1); 



       switch(+eventID) { 


       case '1': 

        String key = LocationManager.KEY_PROXIMITY_ENTERING; 

        Boolean entering = intent.getBooleanExtra(key, false); 

        if (entering) { 
         Log.d(getClass().getSimpleName(), "entering"); 
        } 
        else { 
         Log.d(getClass().getSimpleName(), "exiting"); 
        } 

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

         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0); 

         Notification notification = new Notification(); 

         notification.setLatestEventInfo(context, "Proximity Alert!", "ειναι σωστο αυτο που εκανεσ", pendingIntent); 

         notificationManager.notify(1, notification); 

         notification.icon = R.drawable.ic_menu_notifications; 
         notification.sound = Uri.parse("file:///sdcard/File/sdcard/introduction_file.mp3"); 

         notification.flags |= Notification.FLAG_AUTO_CANCEL; 
         notification.flags |= Notification.FLAG_INSISTENT; 
         notification.flags |= Notification.FLAG_ONGOING_EVENT; 

         notification.ledARGB = Color.WHITE; 
         notification.ledOnMS = 1500; 
         notification.ledOffMS = 1500; 

        break; 

       case '2': 

        String key1 = LocationManager.KEY_PROXIMITY_ENTERING; 

        Boolean entering1 = intent.getBooleanExtra(key1, false); 

        if (entering1) { 
         Log.d(getClass().getSimpleName(), "entering"); 
        } 
        else { 
         Log.d(getClass().getSimpleName(), "exiting"); 
        } 

        NotificationManager nnotificationManager = 
         (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

         PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, null, 0); 

         Notification notification1 = new Notification();  

         notification1.setLatestEventInfo(context, "Proximity Alert!", "ειναι σωστο αυτο που εκανεσ", pendingIntent1); 

         nnotificationManager.notify(2, notification1); 

         notification1.icon = R.drawable.ic_menu_notifications; 
         notification1.sound = Uri.parse("file:///sdcard/File/sdcard/Carter_Lane.mp3"); 

         notification1.flags |= Notification.FLAG_AUTO_CANCEL; 
         notification1.flags |= Notification.FLAG_INSISTENT; 
         notification1.flags |= Notification.FLAG_ONGOING_EVENT; 

         notification1.ledARGB = Color.WHITE; 
         notification1.ledOnMS = 1500; 
         notification1.ledOffMS = 1500; 


        break; 

我已經使用了開關的方法的話,對於不同的事件ID的節目應該改變通知的通知。但它不起作用。你能幫我 非常感謝。

回答

1

您只有一個PendingIntent

引用文檔:

如果創建的應用程序以後重新取回同種的PendingIntent的(相同的操作,目的相同的動作,數據,類別和組件,以及相同的標誌),它將接收代表同樣的道理,如果這是仍然有效

既然你有相同的操作(getBroadcast()),每次在同一Intent路由枚的PendingIntent,只有一個PendingIntent

而不是將動作設置爲PROXIMITY_INTENT_ACTION,使其對每個不同的接近警報都是唯一的。如果您使用Intent上的組件構造函數(即new Intent(this, MyReceiver.class)),則不同的操作字符串不會影響廣播的路由。

+0

首先非常感謝你的回答。我想問如何爲每個點創建不同的通知。第一步是您之前告訴我爲每個點創建不同的PendingIntent並創建意圖(this,MyReceiver.class)。但在廣播接收器類(公共類ProximityAlert擴展BroadcastReceiver){......我怎樣才能指定不同的通知聲音。非常感謝 – amz

+0

@amz:使用不同的ID和不同的Notification對象來調用notify()方法。 – CommonsWare

+0

對不起。我使用了開關方法。如果您看到代碼,對於每個事件ID(可以是1或2),我已創建不同的通知對象,並使用不同的NotifyID將對象註冊到通知管理器。但是當我去測試應用程序時,它不起作用。它不會創建任何通知。你有沒有或可以建議我任何鏈接,我可以獲得有關通知的更多信息。非常感謝你 – amz

相關問題