質問

私はaddProximityAlertメソッドを使用しているAndroidプロジェクトに取り組んでいます。あなたがそれにとても近いならあなたは。

だから私は3日前にそれに取り組んでいました、そして私は再び同じ問題になっていました。

BREF:これは私の簡単なコードです。

#manactivity.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();
          }
     }
}
.

*#問題は、Proximit Pointの近くに近い場合でも、Debuggerが2つの場所の間の距離を表示しても、SorbastReceiver(ProximityReciever)がシステムによって呼び出されることはありませんでした。1000m:/

です

役に立ちましたか?

解決

このトピックについては、AddProximityAlertのサムが機能していない理由を考え出しています。 P>

答えは私の目の前であったが、私はそれに注意を払っていなかったので、私がAndroid officielのドキュメントを読んでいたとき(こちら)私はこの文を見ました" 位置推定のおおよその性質のために、デバイスが所与の領域を短く通過すると、意図が発生しないことが "

になることが可能です。

それはどういう意味ですか?それはあなたがAVD上であなたのアプリをテストしているとき、あなたはDDMからGPS座標(緯度、経度)をAVDに送るのを意味します GPSの実際の側面をシミュレートします(最初の場所では、あなたのプロキシパポイントになることがいくつかのポイントを選んで、その直後に選ぶので、その仕事があるかどうかを確認するために)そしてそれが本物で起こっているものではないものではないデバイス

だから解決策は実際のデバイスであなたのアプリをテストすることです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top