2

好的,所以基本上我想开始监听来自 a 的单个更新BroadcastReceiver,并且由于某种原因,所有提供程序都被禁止访问。这是代码片段

LocationManager locationManager = (LocationManager)
                            context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
String majorProvider = locationManager.getBestProvider(criteria, true);
Log.d("location test", "major provider " + majorProvider);
List<String> allProviders = locationManager.getAllProviders();
for(int i=0; i < allProviders.size();i++) {
    Log.d("location test", "provider " + i + ": " + allProviders.get(i));
}
Log.d("location test", "enabled...");
allProviders = locationManager.getProviders(true);
for(int i=0; i < allProviders.size();i++) {
    Log.d("location test", "enabled provider " + i + ": " + allProviders.get(i));
}

和我的 logcat 结果

04-09 11:23:35.763: D/location test(32576): major provider null
04-09 11:23:35.763: D/location test(32576): provider 0: network
04-09 11:23:35.763: D/location test(32576): provider 1: passive
04-09 11:23:35.763: D/location test(32576): provider 2: gps
04-09 11:23:35.763: D/location test(32576): enabled...

我猜问题是我从 a 调用所有这些BroadcastReceiver,但我尝试从其他地方传递它的上下文。是否无法从 访问 LocationManager BroadcastReceiver

4

2 回答 2

6

哇,我觉得自己像个白痴。好吧,伙计们,这只是一个许可问题。如果有人再次遇到此问题,则只需将使用权限添加到清单

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
于 2012-04-09T08:36:09.810 回答
0

好吧,除了清单中的权限声明外,您可能还需要手动向用户询问与位置相关的权限。授予权限后,调用getProviders| getBestProvider.

String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};    
ActivityCompat.requestPermissions(this, perms, REQ_LOCATION_PERMISSION);
于 2019-10-12T09:14:50.953 回答