我可以在同一类扩展广播接收器中指定不同的通知

时间:2011-07-04 16:31:54

标签: android

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

在BroadcastReceiver类中,我在以下代码中有通知

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;

我使用了switch方法,因此对于不同的eventID,程序应该更改通知。但它不起作用。你能帮助我吗 非常感谢你。

1 个答案:

答案 0 :(得分:1)

您只有一个PendingIntent

引用文档:

  

如果创建应用程序稍后重新检索相同类型的PendingIntent(相同的操作,相同的Intent操作,数据,类别和组件以及相同的标志),它将接收表示相同令牌的PendingIntent,如果它仍然有效

由于每次都有相同的操作(getBroadcast())和相同的Intent路由部分,因此只有一个PendingIntent

不是将操作设置为PROXIMITY_INTENT_ACTION,而是使其对每个不同的邻近警报都是唯一的。如果您在Intent上使用组件构造函数(即new Intent(this, MyReceiver.class)),则不同的操作字符串不会影响广播的路由。