gpt4 book ai didi

java - 尝试在 Android 设备上检索位置...适用于模拟器但不适用于真实设备

转载 作者:行者123 更新时间:2023-11-29 23:25:37 25 4
gpt4 key购买 nike

我的问题:我正在尝试创建一个可以在全校范围内使用的应用程序。它需要不断访问位置。我按照在线教程完成了下面的代码。此代码非常适用于我的 android 模拟器,但不适用于我的设备。我在我的设备上收到了权限请求,但没有任何更新。现在,奇怪的是,如果我在我的设备上使用模拟定位应用程序,这个应用程序就可以工作。但仅限于模拟位置应用程序。

有没有人对如何解决这个问题有任何建议?(xml 文件只是一个 TextView 。就像我说的,我只是先测试一下)

package com.example.dylan.locationexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Location: ", location.toString());

TextView tv = findViewById(R.id.textView);
tv.setText(tv.getText() + Double.toString(location.getLatitude()) + "\t" + Double.toString(location.getLongitude()) + "\n");

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};



if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_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.

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

}else{

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


}
}
}


}

最佳答案

请确保 list 中包含以下内容

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

并在您的物理设备上启用 GPS。

如果您仍然遇到问题,请检查您手机上对该应用程序的权限设置。您可能拒绝了访问位置的权限。

这是我正在使用的代码:

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
com.google.android.gms.location.LocationListener mLocationListener = new com.google.android.gms.location.LocationListener() {
@Override
public void onLocationChanged(final Location location) {
listener.onLocation(location);
}
};
if(location != null) {
listener.onLocation(location);
}else{
locationListener = new android.location.LocationListener() {
@Override
public void onLocationChanged(Location location) {
listener.onLocation(location);
locationManager.removeUpdates(locationListener);
Log.d(TAG,"Location: " + String.valueOf(location.getLongitude()));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}
@Override
public void onProviderEnabled(String s) {

}
@Override
public void onProviderDisabled(String s) {

}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

关于java - 尝试在 Android 设备上检索位置...适用于模拟器但不适用于真实设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com