我正在使用addproximityalert方法的Android项目,正如您已经知道此方法,让您为位置(纬度,经度)和给定的半径给出的位置设置一个接近的警报,并通知如果你如此接近它,你就会。

所以我三天前正在努力,我再次又一次地遇到了同样的问题..

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

*#问题是当我运行该程序时,即使我非常接近Proximit Point,BroadCastreceiver(ProximityReciever)也从未被系统调用,即使调试器会向我显示两个位置之间的距离<1000m:/

有帮助吗?

解决方案

我只是想出了关于这个话题的一些东西,为什么addproximityalert sames不起作用,我正在与你分享这个问题,因为我注意到一些人以前询问了同样的问题,他们没有得到任何答案!

答案只是在我面前,但我没有注意它,所以当我正在阅读Android Officiel文档(

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top