我正在尝试实施简单的Android应用程序,允许用户在用户靠近特定位置时保存位置并显示提醒。然而,我的地理位置似乎并没有触发。我已经花了一些时间,但一直无法找到解决方案。
应用程序应该像这样工作: 1.去位置 2.添加位置提醒 3.当你在附近时获得提醒
MainActivity:
public class MainActivity extends Activity implements LocationListener {
final static int LOCATION_PERMISSION_REQUEST_CODE = 0;
private LocationManager locationManager;
private Location mLocation;
private double lat, lng;
private float radius=1000;
String ACTION_FILTER = "com.example.mikes.locationreminder";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
registerReceiver(new Geofences(), new IntentFilter(ACTION_FILTER));
locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
Intent i= new Intent(ACTION_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
locationManager.addProximityAlert(mLocation.getLatitude(), mLocation.getLongitude(), radius, -1, pi);
}
....
地理围栏:
public class Geofences extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
boolean state = intent.getBooleanExtra(key, false);
if(state){
Log.i("MyTag", "In secret area");
Toast.makeText(context, "In secret area", Toast.LENGTH_SHORT).show();
}else{
Log.i("MyTag", "Left secret area");
Toast.makeText(context, "Left secret area", Toast.LENGTH_SHORT).show();
}
}
}