我是android.i的新手,通過模擬器運行應用程序。但總是出現錯誤.logcat顯示:無法打開堆棧跟蹤文件'/ data/anr/traces。 TXT':權限被拒絕通過gps獲取當前位置(經度和緯度)
WhereAmIActivity.Java
package com.paad.whereami;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class WhereAmIActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);//bulid locationmanager
String provider = LocationManager.KEY_LOCATION_CHANGED;
Location location = locationManager.getLastKnownLocation(provider);//get lastlocation
updateWithNewLocation(location);//pass to updatewithnewlocation
}
private void updateWithNewLocation(Location location){//show Location in TextView
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);//
if(location!=null){//
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:"+lat+"\nLong:"+lng;
}else{//
latLongString = "No location found";
}
myLocationText.setText("Your Current POsition is:\n"+latLongString);//
}
}
使用許可權
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
編寫此權限:<使用權限android:name =「android.permission.INTERNET」/> –
也嘗試使用 –