0

いくつかの広告、ブログ、フォーラムを読みましたが、解決策が見つかりませんでした

私は近接アラートが欲しいので:

public class ProximityIntentReceiver extends BroadcastReceiver {        
@Override
        public void onReceive(Context  context, Intent intent) {
            String key= LocationManager.KEY_PROXIMITY_ENTERING;

            Boolean entering=intent.getBooleanExtra(key, false);
            Log.i(QUEST_PROXIMITY_ALERT,"entering");
            Log.i(QUEST_PROXIMITY_ALERT,"exiting");

        }       
    }  

onCreateMethod で:

 locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 provider=locationmanager.getBestProvider(criteria, true);
 location=locationmanager.getLastKnownLocation(provider);
 if (location!=null) updateWithNewLocation(location);     
 locationmanager.requestLocationUpdates(provider, 0, 0, locationlistener);

そして、私の場所のリスナーは次のとおりです。

private final LocationListener locationlistener= new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.i("LOCATION_UPDATED","");
            updateWithNewLocation(location);
            mapview.invalidate();
    }
        public void onProviderDisabled(String provider) {
            //Message TO-DO
        }

        public void onProviderEnabled(String provider) { }

        public void onStatusChanged(String provider, int sttus, Bundle extras) { }
    };

アクティビティの開始時に、ユーザーにいくつかのデータを選択するためのダイアログを表示します:

//Dialog to choose available EduQuests
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setTitle("Selecciona una búsqueda");
       builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int item) {
               dialog.dismiss();
               mCurrent=item;
               Double lat=Double.parseDouble(mQuests.getItem(item).getPlace(0).getLatitude());
               Double longi=Double.parseDouble(mQuests.getItem(item).getPlace(0).getLongitude());
               GeoPoint point=new GeoPoint((int)(longi*1E6),(int) (lat*1E6));
               SetProximityAlert(point);
               mCurrentPlace=0; //First GeoPoint in the quest
               mQuests.getItem(mCurrent).getPlace(0).setVisibility(true);
               DrawPoints();
               mapview.invalidate();
           }
       });
      AlertDialog alert = builder.create();
      alert.show();   

そして、ダイアログの ClickListener で SetProximityAlert(point) を呼び出します。

 private void SetProximityAlert(GeoPoint point) {    
        double lat=point.getLatitudeE6();
        double lng=point.getLongitudeE6();
        Log.i("SETTING PROXIMITY ALERT LATTITUDE--->",Double.toString(lat));
        Log.i("SETTING PROXIMITY ALERT LONGITUDE--->",Double.toString(lng));
        float radio= 20; //In meters    
        long expiration=-1;

        //Creo el Intent con la acción que hemos definido"
        Intent intent= new Intent(QUEST_PROXIMITY_ALERT);

        //Creo el PendingIntent pasándole
        PendingIntent proximityIntent=PendingIntent.getBroadcast(this,0, intent, 0);
        locationmanager.addProximityAlert(lat, lng, radio, expiration, proximityIntent);

        //Promixity Alert
        IntentFilter filter= new IntentFilter(QUEST_PROXIMITY_ALERT);
        registerReceiver(new ProximityIntentReceiver(),filter);      
    }

正常に動作する UpdateWithNewLocation() メソッドでアラートを発生させる必要があるかどうかを確認するために、距離を計算します。

private void updateWithNewLocation(Location location) {
          Double lat=Double.parseDouble(mQuests.getItem(mCurrent).getPlace(mCurrentPlace).getLatitude());
          Double longi=Double.parseDouble(mQuests.getItem(mCurrent).getPlace(mCurrentPlace).getLongitude());
          Location distlocation=new Location(provider);
          distlocation.setLatitude(lat);
          distlocation.setLongitude(longi);       
          Log.i("DISTANCE TO CURRENT POINT--->",Float.toString(location.distanceTo(distlocation))+"...metros");
          curlat=location.getLatitude();
          curlong=location.getLongitude();
          GeoPoint point=new GeoPoint((int)(curlat*1E6),(int) (curlong*1E6));
          OverlayItem overlayitem=new OverlayItem(point,"I'm here","This is my current position");
          if (curpositemizedoverlay==null) {
              curpositemizedoverlay=new QuestItemizedOverlay(this.getResources().getDrawable(R.drawable.me)); 
          }
          else {
              curpositemizedoverlay.removeItem(curpositemizedoverlay.size()-1);
          }           
          curpositemizedoverlay.addOverlay(overlayitem);
          mapOverlays.add(curpositemizedoverlay);
          mapview.invalidate();
    }

private static String QUEST_PROXIMITY_ALERT="com.pekechis.ieda.proximityalert";

パッケージの名前は com.pekechis.ieda で、メイン アクティビティは IEDAQUEST です。

DDMS でのデバイスの位置を変更すると、現在の位置が変化することがわかります。また、ログに表示されている POI (Point of Interest) までの距離も変更します。

しかし、アラートは発生しません。

私が間違っていることについての考え。

マニフェストでフィルターを宣言するのは問題だと思いましたが、私が読んだように、登録受信者を呼び出す場合はその必要はありません。

前もって感謝します

4

2 に答える 2

0

あなたがまだ見ている場合、これは私にとって魅力のように働きました:http://www.gauntface.co.uk/pages/blog/2010/01/04/proximity-alerts-in-android/

于 2011-06-05T20:59:31.050 に答える
0

何度も試してみましたが、近接アラートを起動することはできませんでした。私は代替ソリューションを選択しました(最適ではないと思います)

現在位置が変わるたびに距離を計算します。私は一度に 1 つの近接アラートしか持っていませんが、多数ある場合、それは最善の解決策ではないと思います。

于 2011-05-19T19:21:10.257 に答える