2

近接アラートを使用していくつかの機能をトリガーするアプリに取り組んでいます。いくつかのチュートリアルと例を掘り下げ、いくつかのアラートを設定しましたが、多少問題ないようです。ただし、アプリを閉じると (locationManager.removeUpdates() を呼び出して更新を受信しなくなります)、アラートが発生し続けているように見えました。

私の質問は、実際にアラーム API に gps 修正を提供する必要があるかどうかです。または、システムはそれを独自に取得し、それらの座標が必要な場合に新しいものを作成しない限り、私はそれについて心配する必要はありませんか?

ありがとう!

4

1 に答える 1

1

I don't think you have to use request location updates - you just unregister your receiver to stop the alerts

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.database.Cursor;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class ProximityService extends Service {

    String proximitysd = "com.apps.ProximityService";
    int n = 0;
    private BroadcastReceiver mybroadcast;
    private LocationManager locationManager;

    public ProximityService() {}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        mybroadcast = new ProximityIntentReceiver();
        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
        double lat;
        double lng;
        float radius = 50f;
        long expiration = -1;
        MyDBAdapter db = new MyDBAdapter(this);
        Cursor cursor;
        db.read();
        cursor = db.getAllEntries();
        boolean go = cursor.moveToFirst();
        while (cursor.isAfterLast() != true) {
            lat = cursor.getInt(MyDBAdapter.LATITUDE_COLUMN) / 1E6;
            lng = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN) / 1E6;
            String what = cursor.getString(MyDBAdapter.ICON_COLUMN);
            String how = cursor.getString(MyDBAdapter.TYPE_COLUMN);
            String proximitys = "com.apps.ProximityService" + n;
            IntentFilter filter = new IntentFilter(proximitys);
            registerReceiver(mybroadcast, filter);
            Intent intent = new Intent(proximitys);
            intent.putExtra("alert", what);
            intent.putExtra("type", how);
            PendingIntent proximityIntent = PendingIntent.getBroadcast(this, n,
                    intent, PendingIntent.FLAG_CANCEL_CURRENT);
            locationManager.addProximityAlert(lat, lng, radius, expiration,
                    proximityIntent);
            // sendBroadcast(new Intent(intent));
            n++;
            cursor.moveToNext();
        }
        db.close();
        cursor.close();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Proximity Service Stopped", Toast.LENGTH_LONG)
                .show();
        unregisterReceiver(mybroadcast);
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Proximity Service Started", Toast.LENGTH_LONG)
                .show();
        // IntentFilter filter = new IntentFilter(proximitys);
        // registerReceiver(mybroadcast,filter);
    }

    public class ProximityIntentReceiver extends BroadcastReceiver {

        private static final int NOTIFICATION_ID = 1000;

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            String key = LocationManager.KEY_PROXIMITY_ENTERING;
            Boolean entering = arg1.getBooleanExtra(key, false);
            String here = arg1.getExtras().getString("alert");
            String happy = arg1.getExtras().getString("type");
            if (entering) {
            }
            NotificationManager notificationManager = (NotificationManager) 
                    getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0,
                    arg1, 0);
            Notification notification = createNotification();
            notification
                    .setLatestEventInfo(arg0, "Proximity Alert!",
                            "You are approaching a " + here + " marker.",
                            pendingIntent);
            notificationManager.notify(NOTIFICATION_ID, notification);
        }

        private Notification createNotification() {
            Notification notification = new Notification();
            notification.icon = R.drawable.icon;
            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.ledARGB = Color.WHITE;
            notification.ledOnMS = 1500;
            notification.ledOffMS = 1500;
            return notification;
        }
        // make actions
    }
}
于 2013-04-02T23:39:51.763 に答える