2

我想要做的是有一个接近警报服务,当你走进半径范围内时,它只会触发一次通知(不停止服务)。每次您进入半径范围内以及每次您走出半径范围时,我的代码都会触发通知。我一直在尝试使用布尔值和 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;
        }

    }
}
4

1 回答 1

0

您应该在触发后删除接近警报,而不是再次重新创建它(将一些标志保存为变量,或者,如果您使用 sqlite,则保存在您的数据库中)。

删除警报有点棘手,需要采取 2 个步骤,两者都会产生不(显然)“触发”的预期结果:

  1. 取消注册您的接收器

    您可以直接保存一个接收器(或接收器数组)unregister

    for (ProximityReceiver proximityReceiverLocal:proximityReceiverArray) {
        context.unregisterReceiver(proximityReceiverLocal);
        proximityReceiverArray.remove(proximityReceiverLocal); // clear pile
    }
    
  2. 删除警报

    注意:您不能将警报另存为对象,您必须重新创建警报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 半径时什么都不发生的预期目标,但您将浪费宝贵的电池寿命和内存。

于 2017-08-23T20:01:25.323 回答