简体   繁体   English

Android如何正确实现接近传感器

[英]Android how to implement Proximity Sensor correctly

My app finds the users location. 我的应用程序找到用户位置。 I'm trying to set a proximity alert. 我正在尝试设置接近警报。 The app scans a qrcode using ZXing scanner. 该应用程序使用ZXing扫描仪扫描二维码。 the qrcode has lon/lat values that i read into app. qrcode具有我读入应用程序的lon / lat值。 i'd like to compare the user's current location against the values stored on the qrcode and determine if the user is within a given tolerance (radius). 我想将用户的当前位置与qrcode上存储的值进行比较,并确定用户是否在给定的公差(半径)范围内。

The onReceive method in ProximityIntentReceiver is never called. 永远不会调用ProximityIntentReceiver中的onReceive方法。 Any ideas why? 有什么想法吗? Thanks. 谢谢。

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

    public class ProximityIntentReceiver extends BroadcastReceiver {

        private static final String TAG = ProximityIntentReceiver.class.getSimpleName(); 
        private static final int NOTIFICATION_ID = 1000;

        @Override
        public void onReceive(Context context, Intent intent) {
             Log.e(TAG, "inside prox onreceive");
            String key = LocationManager.KEY_PROXIMITY_ENTERING;

            Boolean entering = intent.getBooleanExtra(key, false);

            if (entering) {
                Log.d(getClass().getSimpleName(), "entering");
            }
            else {
                Log.d(getClass().getSimpleName(), "exiting");
            }

            NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);       

            Notification notification = createNotification();
            notification.setLatestEventInfo(context,
                "Proximity Alert!", "You are near your point of interest.", pendingIntent);

            notificationManager.notify(NOTIFICATION_ID, notification);

        }

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

            notification.ledARGB = Color.WHITE;
            notification.ledOnMS = 1500;
            notification.ledOffMS = 1500;

            return notification;
        }

    }

.

import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class LocationService extends Service {

    private static final String TAG = LocationService.class.getSimpleName();
    LocationManager             mlocManager;
    LocationListener            mlocListener;
    NfcScannerApplication       nfcscannerapplication;
    private static final String PROX_ALERT_INTENT = "com.carefreegroup.ProximityAlert";
    Intent intent;
    PendingIntent proximityIntent;
    ProximityIntentReceiver pir;

    @Override
    public void onCreate() {

        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        pir = new ProximityIntentReceiver();
        intent = new Intent(PROX_ALERT_INTENT);
        proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        Log.e(TAG, "Service created and location manager and listener created");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        mlocManager.removeUpdates(mlocListener);
        unregisterReceiver(pir);
        Log.e(TAG, "Service destroyed");
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        Log.e(TAG, "requesting location updates");
        super.onStart(intent, startId);
    }

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

    private class MyLocationListener implements LocationListener {


        @Override
        public void onLocationChanged(Location loc) {



            Log.e(TAG, "about to set geopoints in application object");
            nfcscannerapplication.setLat(loc.getLatitude());
            nfcscannerapplication.setLon(loc.getLongitude());

            fireLocationChangeEvent(loc.getLongitude(), loc.getLatitude());

            mlocManager.addProximityAlert(53.653480529785156, -1.51961088180542, 2, -1, proximityIntent );

            IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
            registerReceiver(pir, filter);

              sendBroadcast(intent);

        }

Please, take a look at this Proximity Alert for location coordinates stored in server . 请查看此接近警报,了解服务器中存储的位置坐标

The question is slightly different but the method you need to use it that. 问题稍有不同,但是您需要使用该方法。

I think it's quite more simple. 我认为这要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM