[英]NETWORK_PROVIDER not Working in android
I build app for User to get last location or current location. 我为用户构建应用程序以获取上一个位置或当前位置。 I use both
NETWORK_PROVIDER
and GPS_PROVIDER
.I have little problem with NETWORK_PROVIDER
. 我同时使用
NETWORK_PROVIDER
和GPS_PROVIDER
。我有个小问题NETWORK_PROVIDER
。
1.When i use NEWTWORK_PROVIDER
in Micromax unit 2 than the app crash and when I use GPS_PROVIDER
than app is working good. 1.当我使用
NEWTWORK_PROVIDER
在Micromax的单元2比应用程序崩溃,当我使用GPS_PROVIDER
比应用工作良好。 And same Application is working fine in Samsung and XIOMI devices while using both providers. 同时使用这两个提供程序的同一应用程序在Samsung和XIOMI设备中都可以正常工作。 Any Reason behind this?
这背后有什么原因吗?
Is NETWORK_PROVIDER
supported for only limited version? 仅受限版本支持
NETWORK_PROVIDER
吗?
If some device doesn't support NETWORK_PROVIDER
than what can i do to get current location without GPS? 如果某些设备不支持
NETWORK_PROVIDER
,那么在没有GPS的情况下如何获取当前位置?
Here's my code: 这是我的代码:
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) Requires the permission android.permission.ACCESS_FINE_LOCATION.
gps –>(GPS,AGPS)需要权限android.permission.ACCESS_FINE_LOCATION。
- network –> (AGPS, CellID, WiFi MACID) Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.
网络–>(AGPS,CellID,WiFi MACID)需要android.permission.ACCESS_COARSE_LOCATION或android.permission.ACCESS_FINE_LOCATION权限。
- passive –> (CellID, WiFi MACID) Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes.
被动–>(CellID,WiFi MACID)需要权限android.permission.ACCESS_FINE_LOCATION,尽管如果未启用GPS,则此提供程序可能只会返回粗略的修复。
If you need more informations, you can refer to: http://developerlife.com/tutorials/?p=1375 如果您需要更多信息,可以参考: http : //developerlife.com/tutorials/?p=1375
You can get Gps or Network provider is available or not by below code. 您可以通过以下代码获取Gps或网络提供商是否可用。
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = mLocationManager.isProviderEnabled
(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager.isProviderEnabled
(LocationManager.NETWORK_PROVIDER);
But i strongly recommend you to use fused location. 但我强烈建议您使用融合位置。 Check this and this .
检查这个和这个 。
I say nonsense, but do you use this? 我说废话,但是你用这个吗?
<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" />
Instead of using the legacy LocationManager
, you should use the Google's new Fused Location API connecting to the Google Play Services, you can get the lastknown and current location, here is how to get the lastknownlocation: 而不是使用旧版
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()));
}
}
}
For more details check official example here 有关更多详细信息,请在此处查看官方示例
And in your Manifest.xml: 在您的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.