2016-02-21 72 views
0

我寫了這段代碼,但不幸的是它不起作用。我的設備中的位置服務處於打開狀態。獲取最新已知的地址

代碼:

package com.example.showlocationactivity; 

import android.Manifest; 
import android.app.Activity; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ShowLocationActivity extends Activity implements LocationListener { 

    private TextView latituteField; 
    private TextView longitudeField; 
    private LocationManager locationManager; 
    private String provider; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_location); 
     latituteField = (TextView) findViewById(R.id.TextView02); 
     longitudeField = (TextView) findViewById(R.id.TextView04); 

     // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     Location location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) { 
      System.out.println("Provider " + provider + " has been selected."); 
      onLocationChanged(location); 
     } else { 
      latituteField.setText("Location not available"); 
      longitudeField.setText("Location not available"); 
     } 
    } 

    /* Request updates at startup */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 

    /* Remove the locationlistener updates when Activity is paused */ 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Toast.makeText(this, "Enabled new provider " + provider, 
       Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Toast.makeText(this, "Disabled provider " + provider, 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

權限:

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

結果:

enter image description here

您可以將此代碼複製並粘貼到一個項目並對其進行測試。我該怎麼辦?

+0

您是否嘗試在手機上開啓位置服務?順便說一句,爲了清除你的代碼,使obe方法檢查權限,以便代碼更具可讀性。 – Traabefi

+0

@Traabefi:是的,我檢查了所有的東西 –

+0

嘗試一個不同的實現,因爲這不適合我。我總是在Location location = locationManager.getLastKnownLocation(provider);中得到空值。此方法僅在提供程序被禁用時纔會提供null,這在我的情況中不正確,因此實現中的某些內容是錯誤的,這種情況並非如此。你可以在網上查看教程,其中有很多。 – Aksiom

回答

0

嘗試使用谷歌地圖API Google Map API

+0

我無法運行它。 –

1

如果您最近停用,啓用您的定位服務,或者如果它是第一次在手機試圖獲得一個位置,你會打電話getLastKnownLocation時總是空。如果getLastKnownLocation返回null,請嘗試使用requestSingleUpdate,以便您可以在設備上填充新的位置。