2011-07-04 7 views
0

2つの異なるポイントに対して2つの近接アラートを指定しました。 近接アラートごとに、次のコードに示すようにeventIDを指定しました。各近接警報(それぞれの点について)に対して、私は異種の通知音を出したい。主な質問は、同じクラス内で異なる通知(通知音)を作成する方法です。同じクラスのブロードキャストレシーバー内で異なる通知を指定することはできます

ここに私が使用したコードがあります。私は次のコード

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; 

に通知を持っているBroadcastReceiverクラスの

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

私は別のeventIDプログラムが通知を変更する必要があり、したがってスイッチ法を使用しています。それは動作しません。お手伝いできますか ありがとうございます。

答えて

1

あなたは1つだけPendingIntentを持っています。

ドキュメント引用:作成アプリケーションが後でPendingIntentの同じ種類(同じ操作、同じインテントアクション、データ、カテゴリ、コンポーネント、同じフラグ)を再取得した場合

を、それが受信しますあなたは同じ操作(getBroadcast())と同じIntentルーティング枚毎時間を持っているので、それはまだ

有効であれば、同じトークンを表すPendingIntentは、一つだけPendingIntentあります。

アクションをPROXIMITY_INTENT_ACTIONに設定するのではなく、近接近接アラートごとに一意にする必要があります。 Intent(つまりnew Intent(this, MyReceiver.class))のコンポーネントコンストラクタを使用すると、さまざまなアクション文字列がブロードキャストのルーティングに影響を与えません。

+0

まずはお返事ありがとうございます。私はどのように私は各ポイントごとに異なる通知を作成することができます尋ねたいと思います。最初のステップは、各ポイントに対して異なるPendingIntentを作成し、インテント(これはMyReceiver.class)を作成するために私が前に言ったことです。しかし、ブロードキャストレシーバクラス(PublicクラスProximityAlertはBroadcastReceiverを継承しています){......どうすれば別の通知音を指定できますか?どうもありがとうございます。 – amz

+0

@amz: 'notify()'メソッドへの呼び出しに、異なるIDと異なる 'Notification'オブジェクトを使用してください。 – CommonsWare

+0

もう一度申し訳ありません。私はスイッチメソッドを使用しました。コードが表示された場合は、イベントIDごとに(1または2)、異なる通知オブジェクトを作成し、異なるNotifyIDで通知マネージャにオブジェクトを登録します。しかし、私はアプリケーションをテストするために行くときは動作しません。それは通知を作成しません。あなたは、私が通知に関するより多くの情報を得ることができるリンクを持っているか、あなたに教えてもらえますか?どうもありがとうございました – amz

関連する問題