我为用户构建应用以获取最后位置或当前位置。我同时使用NETWORK_PROVIDER
和GPS_PROVIDER
。我对NETWORK_PROVIDER
没有什么问题。
1.当我在Micromax单元2中使用NEWTWORK_PROVIDER
而不是应用程序崩溃时,当我使用GPS_PROVIDER
时,应用程序运行正常。使用两个提供商时,同样的应用程序在三星和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);
}
答案 0 :(得分:4)
- 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
答案 1 :(得分:3)
您可以通过以下代码获得Gps或网络提供商。
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = mLocationManager.isProviderEnabled
(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager.isProviderEnabled
(LocationManager.NETWORK_PROVIDER);
答案 2 :(得分:2)
我说废话,但你用这个吗?
<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" />
答案 3 :(得分:2)
您应该使用Google的新Fused Location API连接到Google Play服务,而不是使用旧版LocationManager
,您可以获取上次知道的当前位置,以下是如何获取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()));
}
}
}
有关详细信息,请查看官方示例here
在你的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>