我已经遇到这个问题好几天没有任何改进和希望这个代码工作。 addProximityAlert()
locationmanager
的{{1}}不会在大于16的API
级别被触发或触发。它在API level 16
中完美运行,但在 API 17上无效。
method
中的deprecated
是higher api
还是我错过了什么?
broadcastreciever
完全正常,如果手动调用则有效。
pendingintent
也很好,并且在手动启动时有效。
这里似乎唯一的问题是addProximityAlert()
本身。
关于这个主题还有很多其他问题,但似乎没有人回答这个问题。如果有人帮助我解决这个问题,那真的很高兴。
我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
double lat = 37.422000;
double lon = -122.084095;
float radius = 5.0f * 1609.0f;
Intent intent = new Intent(PROX_ALERT);
pIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,new MyLocationListener());
locMgr.addProximityAlert(lat, lon, radius, 2000l, pIntent1);
proxReceiver = new ProximityReceiver();
IntentFilter iFilter = new IntentFilter(PROX_ALERT);
registerReceiver(proxReceiver, iFilter);
}
class ProximityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
Log.d("==>","broadcast recieved");
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d("==>", "entering");
}
else {
Log.d("==>", "exiting");
}
}
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Location pointLocation = getLocation();
float distance = location.distanceTo(pointLocation);
Toast.makeText(MainActivity.this, "Distance from Point:" + distance, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
提前致谢!
答案 0 :(得分:0)
来自文档:
在API版本17之前,可以使用此方法 ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION。从API版本17开始 此后,此方法需要ACCESS_FINE_LOCATION权限。