简体   繁体   English

Java Android Studio-检查权限

[英]java android studio - check permission

i want to get coordinat and i'm use function GPSTracker (follow tutorial https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ ) but any issue... i can't get coordinates, please help me guys 我想获得协调并且我正在使用GPSTracker函数(遵循教程https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ ),但是任何问题...我都无法得到坐标,请帮助我

this is my acitivity : 这是我的积极性:

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();
            }
        }
    });
}

This is file GPSTracker.java : 这是文件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;
}
 }

My problem with access permissions row : 我的访问权限行问题:

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();
                        }
                    }
                }

Don't use GPSTracker. 不要使用GPSTracker。 The code is broken- it will sometimes work, but its flawed at a design level. 该代码已损坏-有时可以工作,但是在设计级别存在缺陷。 It confuses enabled with up and running, and doesn't account for the fact you can't always get locations. 它使启用与运行混淆,并且不能解释您无法始终获取位置的事实。 For a full explanation of all the ways its broken, see my blog at http://gabesechansoftware.com/location-tracking/ which also contains working code. 有关其损坏的所有方式的完整说明,请参阅我的博客, 网址http://gabesechansoftware.com/location-tracking/ ,其中也包含有效的代码。

Also, you need runtime permissions (which aren't in my blog either, as it predates them). 另外,您需要运行时权限(在我的博客中也没有,因为它早于它们)。 You'll need to add those in regardless. 无论如何,您都需要添加这些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM