2012-04-09 112 views
2

好吧,所以基本上我想開始監聽BroadcastReceiver的單個更新,出於某種原因,所有提供程序都禁止訪問。這裏是代碼片段LocationManager getProviders(true)不返回提供商

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... 

我猜測的問題是,我從BroadcastReceiver呼籲所有這一切,但我想通過它的上下文從別的地方。是否可以從BroadcastReceiver訪問LocationManager?

+0

所有供應商都啓用或不是...? – ngesh 2012-04-09 08:35:21

+0

@sandy他們都被禁用,它只是一個被遺忘的權限 – MikeIsrael 2012-04-09 08:36:40

回答

6

哇,我覺得這樣的白癡。那麼,這只是一個權限問題。如果有人再次遇到此問題,只需將使用權限添加到清單

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
+1

我有我的應用程序的權限(包括在運行時詢問),仍然有問題。 :( – Renann 2017-01-01 17:47:56