可一个BroadcastReceiver赶上多个节目? [英] Can a broadcastReceiver catch multiple broadcasts?

查看:116
本文介绍了可一个BroadcastReceiver赶上多个节目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建多个接近警报,但我不能得到它的工作...

I am trying to create multiple proximity alerts but I cant get it to work...

我认为广播接收器被覆盖,因此仅处理的最后一个播出。所以,如果我有两个点附近只有一个,其目的是创建最后将生成警报...

I think that the broadcast receiver gets overwritten and thus is handling only the last broadcast. So if I had two points close by only the one whose intent was created last will generate an alert...

我读,我应该使用要求codeS,但我不知道如何做到这一点?

I read that I should use request codes but I have no idea on how to do that...

我的方法用于设置待处理的意图和广播接收器...

My method for setting up the pending intents and the broadcast receiver...

private void addProximityAlert(double latitude, double longitude, String poiName, String intentfilter) {

    Bundle extras = new Bundle();
    extras.putString("name", poiName);
    Intent intent = new Intent(PROX_ALERT_INTENT+poiName);  
    intent.putExtras(extras);       
    PendingIntent proximityIntent = PendingIntent.getBroadcast(MainMenu.this, requestCode, intent, 0);
    locationManager.addProximityAlert(
        latitude, // the latitude of the central point of the alert region
        longitude, // the longitude of the central point of the alert region
        POINT_RADIUS, // the radius of the central point of the alert region, in meters
        PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
        proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
   );
    requestCode++;
   IntentFilter filter = new IntentFilter(intentfilter); 
   registerReceiver(new ProximityIntentReceiver(), filter);
}

我的BroadcastReceiver类

My broadcastreceiver class

public class ProximityIntentReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1000;

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

    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 = createNotification();        
    notification.setLatestEventInfo(context, 
        "Proximity Alert!", "You are approaching: " +intent.getExtras().get("name"), pendingIntent);     
                                                                        //here-------------------------------------
    notificationManager.notify(NOTIFICATION_ID, notification);

}

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

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

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

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

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

    return notification;
}

}

你能帮帮我吗?我真的坚持这个...

Can you please help me??? I'm really stuck with this...

任何帮助将是非常美联社preciated!

Any help would be really appreciated!!!

推荐答案

我得到它的工作毕竟...

i got it working after all...

正是我一直在寻找: http://www.gauntface.co.uk/blog/2010/01/04/proximity-alerts-in-android/

修改我发

private void addProximityAlert(double latitude, double longitude, String poiName) {

    Bundle extras = new Bundle();
    extras.putString("name", poiName);
    extras.putInt("id", requestCode);
    Intent intent = new Intent(PROX_ALERT_INTENT);
    intent.putExtra(PROX_ALERT_INTENT, extras);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(MainMenu.this, requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.addProximityAlert(
        latitude, // the latitude of the central point of the alert region
        longitude, // the longitude of the central point of the alert region
        POINT_RADIUS, // the radius of the central point of the alert region, in meters
        PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
        proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
   );
    requestCode++;       
}

private void initialiseReceiver()
{
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
    registerReceiver(new ProximityIntentReceiver(), filter);
}


package michaels.pack;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1000;

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

    String key = LocationManager.KEY_PROXIMITY_ENTERING;

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

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

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

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

    Notification notification = createNotification();        
    notification.setLatestEventInfo(context, 
        "Proximity Alert!", "You are approaching: " +intent.getBundleExtra("michaels.pack.ProximityAlert.").get("name"), pendingIntent);     
                                                                        //here-------------------------------------
    notificationManager.notify( intent.getBundleExtra("michaels.pack.ProximityAlert.").getInt("id"), notification);

}

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

    notification.icon = R.drawable.androidmarker;
    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_LIGHTS;
    notification.defaults |= Notification.DEFAULT_SOUND;

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

    return notification;
}

}

这篇关于可一个BroadcastReceiver赶上多个节目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆