NETWORK_PROVIDER 未在 android 工作
NETWORK_PROVIDER not Working in android
我为用户构建应用程序以获取上次位置或当前位置。我同时使用 NETWORK_PROVIDER
和 GPS_PROVIDER
。我对 NETWORK_PROVIDER
没什么问题。
1.When 我在 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);
}
我说废话,你用这个吗?
<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" />
您应该使用 Google 的新融合位置 API 连接到 Google Play 服务,而不是使用旧版 LocationManager
,您可以获得最后已知位置和当前位置,这里是如何获取最后已知位置:
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>
- Micromax Unit 2 有一个 A-GPS,所以如果应用程序崩溃很奇怪:你有更多关于这个的日志吗?
- 对我来说,有3种情况:
- gps –> (GPS, AGPS) Requires the permission
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.
- 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.
如果您需要更多信息,可以参考: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);
我为用户构建应用程序以获取上次位置或当前位置。我同时使用 NETWORK_PROVIDER
和 GPS_PROVIDER
。我对 NETWORK_PROVIDER
没什么问题。
1.When 我在 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);
}
我说废话,你用这个吗?
<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" />
您应该使用 Google 的新融合位置 API 连接到 Google Play 服务,而不是使用旧版 LocationManager
,您可以获得最后已知位置和当前位置,这里是如何获取最后已知位置:
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>
- Micromax Unit 2 有一个 A-GPS,所以如果应用程序崩溃很奇怪:你有更多关于这个的日志吗?
- 对我来说,有3种情况:
- gps –> (GPS, AGPS) Requires the permission 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.
- 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.
如果您需要更多信息,可以参考: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);