我正在尝试创建一个可以打印我当前的逻辑和纬度的应用,但我无法让它工作。我的代码是这样的: `public class MainActivity扩展了Activity {
private Button but = null;
private TextView txt=null;
String mprovider;
private static final String TAG = "Debug";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
mprovider = locationManager.getBestProvider(criteria, false);
but = (Button) findViewById(R.id.btnLocation);
txt=(TextView)findViewById(R.id.textView);
txt.setText(mprovider);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
Location location=locationManager.getLastKnownLocation(mprovider);
txt.setText(location.getLatitude()+" "+ location.getLongitude());
}
}
});
} }`
我基于这里的代码 http://www.viralandroid.com/2015/12/how-to-get-current-gps-location-programmatically-in-android.html 这也不起作用 当我下载应用程序打印我的坐标时,它工作,但我注意到它要求我允许访问我的位置,我的应用程序没有做的事情。我错过了什么? 清单是添加了这些行的默认值: