簡體   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