4

私は AddProximityAlert メソッドを使用している Android プロジェクトに取り組んでいます。このメソッドを使用すると、位置 (緯度、経度) と指定された半径で指定された場所に近接アラートを設定できることを既に知っているため、それにとても近いです。

だから私は3日前にそれに取り組んでいましたが、同じ問題が何度も発生していました..

in bref: これは私の単純なコードです。

#MainActivity.java

package com.example.proximityalert;

import android.app.Activity;
import android.app.PendingIntent;
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.util.Log;

public class MainActivity extends Activity implements LocationListener {

    LocationManager lm;

    //Defining Latitude & Longitude
    double lat=37.422006 ,long1=-122.084095;
    //Defining Radius
    float radius=1000;               

    //Intent Action 
    String ACTION_FILTER = "com.example.proximityalert";


   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       //i'm registering my Receiver First
       registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));

       //i'm calling ther service Location Manager
       lm=(LocationManager) getSystemService(LOCATION_SERVICE);

       //for debugging...
       lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);

       //Setting up My Broadcast Intent
       Intent i= new Intent(ACTION_FILTER);
       PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);

       //setting up proximituMethod
       lm.addProximityAlert(lat, long1, radius, -1, pi);

   }

@Override
//just For debugging to See the distance between my actual position and the aproximit point  
public void onLocationChanged(Location newLocation) {

    Location old = new Location("OLD");
    old.setLatitude(lat);
    old.setLongitude(long1);

    double distance = newLocation.distanceTo(old);

    Log.i("MyTag", "Distance: " + distance);
}

@Override
public void onProviderDisabled(String arg0) {}

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

}

#ProximityReceiver.java

package com.example.proximityalert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;


public class ProximityReciever extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         // Key for determining whether user is leaving or entering 
         String key = LocationManager.KEY_PROXIMITY_ENTERING;

         //Gives whether the user is entering or leaving in boolean form  
         boolean state = intent.getBooleanExtra(key, false);

          if(state){
                // Call the Notification Service or anything else that you would like to do here
                Log.i("MyTag", "Welcome to my Area");
                Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
          }else{
              //Other custom Notification 
                Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
                Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
          }
     }
}

*#問題は、プログラムを実行すると、ブロードキャストレシーバー (ProximityReciever) がシステムによって呼び出されることはなく、近接ポイントに非常に近い場合でも、デバッガーが 2 つの場所の間の距離が 1000m 未満であることを示している場合でもです。 :/

4

1 に答える 1

4

私はちょうどこのトピックについて何かを理解し、なぜ addProximityAlert が同じように機能しないのかを理解しました。これをあなたと共有しているのは、以前に同じ質問をした人が何の回答も得ていないことに気付いたからです!

答えは目の前にあったのに気に留めていなかったので、Android の公式ドキュメント (ここ) を読んでいたときに、この文が表示されました。与えられた領域が短い間、インテントが発射されない可能性があります

それはどういう意味ですか?これは、AVD でアプリをテストしていて、GPS 座標 (緯度、経度) を DDMS から AVD に送信する場合、GPS の実際の側面をシミュレートするのが非常に難しいことを意味します (最初にいくつかのポイントを選択するため)。あなたのproximPointになり、その直後にproximPointから非常に遠く離れたAnthor Pointを選択して、それが機能するかどうかを確認します)、それは実際のデバイスで起こっていることではありません.

したがって、解決策は、実際のデバイスでアプリをテストするか、DDMS を使用して、必要なゾーンになるまで座標を非常にゆっくりと変更することです。

于 2014-04-12T22:08:37.227 に答える