2

私がやろうとしているのは、半径内に足を踏み入れたときに(サービスを停止せずに)1回だけ通知をトリガーする近接アラートサービスを用意することです。私のコードは、半径の内側に足を踏み入れるたびに通知をトリガーし、半径の外に出るたびに通知をトリガーします。ブール値と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