繁体   English   中英

NETWORK_PROVIDER在android中不起作用

[英]NETWORK_PROVIDER not Working in android

我为用户构建应用程序以获取上一个位置或当前位置。 我同时使用NETWORK_PROVIDERGPS_PROVIDER 。我有个小问题NETWORK_PROVIDER

1.当我使用NEWTWORK_PROVIDER在Micromax的单元2比应用程序崩溃,当我使用GPS_PROVIDER比应用工作良好。 同时使用这两个提供程序的同一应用程序在Samsung和XIOMI设备中都可以正常工作。 这背后有什么原因吗?

  1. 仅受限版本支持NETWORK_PROVIDER吗?

  2. 如果某些设备不支持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);
}
  1. Micromax Unit 2带有A-GPS,因此如果应用崩溃,这很奇怪:您是否有更多关于此的日志?
  2. 对我来说,有3种情况:
  1. gps –>(GPS,AGPS)需要权限android.permission.ACCESS_FINE_LOCATION。
  2. 网络–>(AGPS,CellID,WiFi MACID)需要android.permission.ACCESS_COARSE_LOCATION或android.permission.ACCESS_FINE_LOCATION权限。
  3. 被动–>(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM