2016-02-21 39 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,以便您可以在设备上填充新的位置。