gpt4 book ai didi

android - broadcastReceiver 可以捕获多个广播吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:01 25 4
gpt4 key购买 nike

我正在尝试创建多个接近警报,但我无法让它工作...

我认为广播接收器被覆盖,因此只处理最后一个广播。因此,如果我只有两个点靠近最后创建 Intent 的那个点,则会生成警报...

我读到我应该使用请求代码,但我不知道该怎么做...


我设置挂起 Intent 和广播接收器的方法...

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

我的广播接收者类

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

}

你能帮帮我吗???我真的被这个困住了......

任何帮助将不胜感激!!!

最佳答案

毕竟我让它工作了......

正是我要找的: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 initializeReceiver()
{
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;
}

}

关于android - broadcastReceiver 可以捕获多个广播吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4942398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com