Android-通知后删除邻近警报 [英] Android - remove Proximity Alert after notification

查看:100
本文介绍了Android-通知后删除邻近警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是拥有一个接近警报服务,当您进入半径范围内时(仅在不停止服务的情况下),该通知仅触发一次通知。每当您在半径范围内或每次超出半径范围时,我的代码都会触发通知。我一直在尝试使用布尔值和removeProximityAlert,但没有成功。有任何想法吗?

what I am trying to do is have a proximity alert service which triggers a notification ONLY ONCE when you step inside the radius (without stopping the service). my code triggers notifications every time you step inside the radius and every time you step outside the radius. i've been trying with booleans and with removeProximityAlert, but no success. any ideas?

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

    }
}


推荐答案

您应该在接近警报发出后将其删除,而不要重新创建(将一些标志作为变量保存,或者,如果使用sqlite,请在数据库中保存)。

You should remove the proximity alert after it fires and not recreate it again (save some flag, as a variable or, if you use sqlite, in your db).

删除警报有些棘手,需要执行两个步骤,并且两个步骤都会产生预期的结果(显然不是)触发:

Removing alerts is a bit tricky, there are 2 steps to take and both will produce the intended result of not (apparently) "firing":


  1. 取消注册接收者

  1. Unregister your receiver

您可以保存一个接收者(或接收者数组)并取消注册它直接:

You may save a receiver (or array of receivers) and unregister it directly:

for (ProximityReceiver proximityReceiverLocal:proximityReceiverArray) {
    context.unregisterReceiver(proximityReceiverLocal);
    proximityReceiverArray.remove(proximityReceiverLocal); // clear pile
}


  • 删除警报

  • Remove alerts

    注意:您无法将警报另存为对象,必须重新创建 PendingIntent 并将其提交到其 removeProximityAlert 方法。待定意图必须具有相同的意图(即,相同的操作名称)和相同的待定意图ID:

    Note: You cannot save an alert as an object, you must recreate your PendingIntent and submit it to its removeProximityAlert method. The pending intent must have the same intent (i.e. same action name) and the same pending intent 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的半径时,您将实现不发生任何事情的预期目标,但是这将浪费宝贵的电池寿命和内存。

    If you remove one and not the other, you will achieve your intended goal of nothing occurring when you enter the POI's radius, but you will be wasting precious battery life and memory.

    这篇关于Android-通知后删除邻近警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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