我要做的是拥有一个接近警报服务,当您进入半径(不停止服务)时,该服务仅触发通知。每次进入半径并且每次走出半径时,我的代码都会触发通知。我一直在尝试使用布尔值和removeProximityAlert,但没有成功。任何想法?
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class ProximityService extends Service {
private String PROX_ALERT_INTENT = "com.example.proximityalert";
private BroadcastReceiver locationReminderReceiver;
private LocationManager locationManager;
private PendingIntent proximityIntent;
@override
public void onCreate() {
locationReminderReceiver = new ProximityIntentReceiver();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
double lat = 55.586568;
double lng = 13.0459;
float radius = 1000;
long expiration = -1;
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(locationReminderReceiver, filter);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("alert", "Test Zone");
proximityIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
locationManager.addProximityAlert(lat, lng, radius, expiration, proximityIntent);
}
@override
public void onDestroy() {
Toast.makeText(this, "Proximity Service Stopped", Toast.LENGTH_LONG).show();
try {
unregisterReceiver(locationReminderReceiver);
} catch (IllegalArgumentException e) {
Log.d("receiver", e.toString());
}
}
@override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Proximity Service Started", Toast.LENGTH_LONG).show();
}
@override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@suppressWarnings("deprecation")
@override
public void onReceive(Context arg0, Intent arg1) {
String place = arg1.getExtras().getString("alert");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0, arg1, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(arg0, "Entering Proximity!", "You are approaching a " + place + " marker.", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
locationManager.removeProximityAlert(proximityIntent);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
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_SOUND;
return notification;
}
}
}
答案 0 :(得分:0)
你应该在它触发后删除接近警报,而不是再次重新创建它(保存一些标志,作为变量,或者,如果你在数据库中使用sqlite)。
删除警报有点棘手,需要采取两个步骤,两者都会产生预期的结果(显然)"触发":
取消注册您的接收器
您可以直接保存接收器(或接收器阵列)和unregister
:
for (ProximityReceiver proximityReceiverLocal:proximityReceiverArray) {
context.unregisterReceiver(proximityReceiverLocal);
proximityReceiverArray.remove(proximityReceiverLocal); // clear pile
}
删除提醒
注意:您无法将警报另存为对象,必须重新创建PendingIntent
并将其提交到removeProximityAlert
方法。待处理的意图必须具有相同的意图(即相同的action name
)和相同的待处理意图ID:
public void removeProximityAlert(int pendingIntentIdIn, String intentActionNameIn) {
Intent intent = new Intent(intentActionNameIn);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context , pendingIntentIdIn, intent, 0);
locationManager.removeProximityAlert(pendingIntent);
}
如果您删除了一个而不是另一个,那么当您进入POI的半径时,您将达到预期的目标,但是您将浪费宝贵的电池寿命和记忆。