[英]NETWORK_PROVIDER not Working in android
我為用戶構建應用程序以獲取上一個位置或當前位置。 我同時使用NETWORK_PROVIDER
和GPS_PROVIDER
。我有個小問題NETWORK_PROVIDER
。
1.當我使用NEWTWORK_PROVIDER
在Micromax的單元2比應用程序崩潰,當我使用GPS_PROVIDER
比應用工作良好。 同時使用這兩個提供程序的同一應用程序在Samsung和XIOMI設備中都可以正常工作。 這背后有什么原因嗎?
僅受限版本支持NETWORK_PROVIDER
嗎?
如果某些設備不支持NETWORK_PROVIDER
,那么在沒有GPS的情況下如何獲取當前位置?
這是我的代碼:
private void beginAcquiringLocation() {
//Log.d(TAG, "Beginning location acquisition");
logStatus("Requesting location update.");
// we don't want to be killed while handling data transmission in another thread
// to guarantee we don't take an eternal lock even in case of bugs, set a timeout
acquireWakeLock(WAKELOCK_TIME_DEFAULT);
LocationManager locationManager =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
String provider = locationManager.getBestProvider(crit, false);
Location location = locationManager.getLastKnownLocation(provider);
Log.d("LOCATION>>>>>>>>>>>", String.valueOf(location));
Log.d("Provider---------->>>>>>>>", provider);
// no significant time or distance minima, we'll decide if it's usable when we get a coord
locationManager.requestLocationUpdates(provider, 500, 0, this);
if(String.valueOf(location).equals("null")){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
Location l=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("-------Location default----->>>>>>>", String.valueOf(l));
}
// next action in process is in the callback upon receiving a location update
// to prevent endless listening if no updates are ever received, we need to schedule a timeout
timeoutHandler.removeCallbacks(timeoutTask);
timeoutHandler.postDelayed(timeoutTask, timeoutDelay);
}
- gps –>(GPS,AGPS)需要權限android.permission.ACCESS_FINE_LOCATION。
- 網絡–>(AGPS,CellID,WiFi MACID)需要android.permission.ACCESS_COARSE_LOCATION或android.permission.ACCESS_FINE_LOCATION權限。
- 被動–>(CellID,WiFi MACID)需要權限android.permission.ACCESS_FINE_LOCATION,盡管如果未啟用GPS,則此提供程序可能只會返回粗略的修復。
如果您需要更多信息,可以參考: http : //developerlife.com/tutorials/?p=1375
您可以通過以下代碼獲取Gps或網絡提供商是否可用。
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = mLocationManager.isProviderEnabled
(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager.isProviderEnabled
(LocationManager.NETWORK_PROVIDER);
我說廢話,但是你用這個嗎?
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
而不是使用舊版LocationManager
,您應該使用連接到Google Play服務的Google新的融合位置API,您可以獲取最近的位置和當前位置,以下是獲取lastknownlocation的方法:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
...
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
}
有關更多詳細信息,請在此處查看官方示例
在您的Manifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.basiclocationsample" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.