gpt4 book ai didi

java android studio - 检查权限

转载 作者:行者123 更新时间:2023-12-02 12:36:14 25 4
gpt4 key购买 nike

我想获取坐标,并且我正在使用函数 GPSTracker(按照教程 https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ ),但有任何问题...我无法获取坐标,请帮助我

这是我的 Activity :

public class InputDataSlo extends AppCompatActivity {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GPSTracker gps = new GPSTracker (InputDataSlo.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude= gps.getLongitude();
textView.setText("Latitude: "+latitude+" Longitude: "+longitude);
}else{
Toast.makeText(getApplicationContext(),
"Please cek permision", Toast.LENGTH_LONG)
.show();
}
}
});
}

这是文件 GPSTracker.java :

public class GPSTracker extends Service implements LocationListener {

private Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;
double latitude, longitude;

LocationManager locationManager;
AlertDialogManager am = new AlertDialogManager();

public GPSTracker(Context context) {
this.context = context;
getLocation();
}

private Location getLocation() {
// TODO Auto-generated method stub
try {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (isGPSEnabled) {
this.canGetLocation = true;
if (location == null) {
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
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 3, this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}

}
} else {
showAlertDialog();
}

}catch(Exception e){
e.printStackTrace();
}
return location;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(GPSTracker.this);

// Setting Dialog Title
alertDialog.setTitle("GPS is settings");

// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);

// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.cancel();
}
});

// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

// Showing Alert Message
alertDialog.show();
}

public void showAlertDialog(){
am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
}
public double getLatitude(){
if (location != null){
latitude = location.getLatitude();
}

return latitude;
}

public double getLongitude(){
if (location != null){
longitude = location.getLongitude();
}

return longitude;
}

public boolean canGetLocation(){
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null){
this.location = location;
}
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

我的访问权限行问题:

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
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 3, this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}

最佳答案

不要使用 GPSTracker。代码被破坏了——它有时会起作用,但在设计层面上有缺陷。它混淆了启用与启动和运行,并且没有考虑到您不能总是获取位置的事实。有关其损坏方式的完整说明,请参阅我的博客 http://gabesechansoftware.com/location-tracking/其中还包含工作代码。

此外,您还需要运行时权限(这也不在我的博客中,因为它早于它们)。无论如何,您都需要添加它们。

关于java android studio - 检查权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121552/

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