我写了一点
安卓
应显示智能手机当前位置(上次已知位置)的应用程序。尽管我复制了示例代码,并尝试了其他几种解决方案,但似乎每次都有相同的错误。我的应用程序由一个按钮组成。按下按钮应该
日志
经度和纬度,但仅限日志
..。
下面是
MainActivity.java
public class MainActivity extends Activity {
int response;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onbutclick();
}
});
}
public void onbutclick(){
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc != null){
Log.d("Latitude",Double.toString(loc.getLatitude()));
Log.d("Longitude",Double.toString(loc.getLongitude()));
}
}else{
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},response);
Intent inte = getIntent();
finish();
startActivity(inte);
}
}我也在Manifest文件中使用了。我真的很感激你能给我解释一下
真正的意思是。
发布于 2021-02-28 16:44:40
而不是:
log.d(...);使用
log.i(...);正如其他答案所建议的,您需要特殊权限才能读取调试消息。
https://stackoverflow.com/questions/38587844
复制相似问题