2017-08-30 161 views
1

您好,我是android開發新手。我想獲得當前位置,但我需要等待聽衆開始工作。問題是我沒有改變位置,所以聽衆不能工作(?)!如何在位置不變的情況下獲取當前位置

這裏是我的代碼

我的聽衆樣本:

locationListener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

       textView.append(location.getLongitude() + "\n" + location.getLatitude() + "\n"); 
       // just monitoring 
       lat = location.getLatitude(); 
       lng = location.getLongitude(); 

      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 

      } 
     }; 

,我獲得此

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 0, locationListener); 

更新所以,如果我沒有記錯的GPS將採取新的位置基於時間(300毫秒)或基於距離是0,所以它會工作?我應該等一段時間開始服用嗎?多久?超過2分鐘?

我得到的Wi-Fi打開,當然GPS,這是我的清單

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".Details" /> 
    <activity android:name=".LoginMenu" /> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 

    <activity android:name=".Road" /> 
    <activity android:name=".LoopActivity" /> 
    <activity android:name=".Pedestrians"></activity> 
</application> 

的問候非常感謝

+0

嘗試將權限從「ACCESS_COARSE_LOCATION」更改爲「ACCESS_FINE_LOCATION」。 – iTurki

+0

我得到了他們倆。你的意思是刪除一個? –

回答

0
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, 
         Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); 
      } 
     } 
     LocationManager mLocationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); 
     if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      mProvider = GPS_PROVIDER; 
     } else { 
      mProvider = NETWORK_PROVIDER; 
         } 
     mLocationManager.requestLocationUpdates(mProvider, REFRESH_TIME, REFRESH_DISTANCE, mLocationListener); 
     Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      getAddressFromLocation(mLastLocation, getContext(), new GeocoderHandler()); 
     } 

這就是我在應用程序中獲得類似結果的方式,該設備可能坐在桌子上,但它會定期刷新位置。下面的方法是我對數據進行解碼:

public void getAddressFromLocation(
      final Location location, final Context context, final Handler handler) { 
     if (context != null) { 
      Thread thread = new Thread() { 
       @Override 
       public void run() { 
        Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
        String result = null; 
        try { 
         List<Address> list = geocoder.getFromLocation(
           location.getLatitude(), location.getLongitude(), 1); 
         if (list != null && list.size() > 0) { 
          Address address = list.get(0); 
          // sending back first address line and locality 
          //result = address.toString(); 
          result = address.getAddressLine(0) + ", " + address.getLocality(); 
          mAddress = address; 
          Log.e(TAG, result); 
         } 
        } catch (IOException e) { 
         Log.e(TAG, "Impossible to connect to Geocoder", e); 
        } finally { 
         Message msg = Message.obtain(); 
         msg.setTarget(handler); 
         if (result != null) { 
          msg.what = 1; 
          Bundle bundle = new Bundle(); 
          bundle.putString("address", result); 
          msg.setData(bundle); 
          Log.e(LocationFragment.class.getSimpleName(), "getAddressFromLocation"); 
         } else 
          msg.what = 0; 
         msg.sendToTarget(); 
        } 
       } 
      }; 
      thread.start(); 
     } 
    } 

中使用全局變量定義如下:

private Address mAddress; 
private String mProvider = LocationManager.GPS_PROVIDER; 

我前一段時間面臨着類似的問題,這也許是所有的高潮知識和我在互聯網上收集的代碼。它符合我的目的,現在我很滿意,你也可以改進它。