调用中的类函数 android stuidio

标签: Java Android
发布时间: 2017/3/19 18:23:57
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在进行的项目,一类为 GPS

我知道,要调用的类我必须使用即时类的

像这种事情

GPS insgps = new GPS();
if (insgps .canGetLocation())
{/* Do Something */}

但当我尝试使用这段代码时出现错误

GPS 具有私有访问权限在 'com.myapp.locationapp.app.GPS'

我不知道为什么和如何解决这个问题吗?

这里是我使用的类

        import android.Manifest;
        import android.app.Activity;
        import android.app.AlertDialog;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.content.pm.PackageManager;
        import android.location.Location;
        import android.location.LocationListener;
        import android.location.LocationManager;
        import android.os.Bundle;
        import android.provider.Settings;
        import android.support.v4.app.ActivityCompat;

public final class GPS implements LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
    private static GPS _instance = new GPS();
    private static Activity _activity;

    private static boolean _isGPSEnabled = false;
    private static boolean _isNetworkEnabled = false;
    private static boolean _canGetLocation = false;
    private static boolean _isPermissionEnabled = false;

    private Location _location;
    private double _latitude;
    private double _longitude;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters
    private static final long MIN_TIME_BW_UPDATES = 1; // 1 minute

    private static LocationManager _locationManager;

    private LocationPermissionResponseListener _locationPermissionListener;
    public static final int LOCATION_REQUEST_CODE = 200;

    private GPS() {}

    public static GPS sharedInstance(Activity activity) {
        _activity = activity;
        _locationManager = (LocationManager) _activity.getSystemService(Context.LOCATION_SERVICE);
        _isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        _isNetworkEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!_isGPSEnabled && !_isNetworkEnabled) {
            _canGetLocation = false;
        } else {
            _canGetLocation = true;
        }

        if (ActivityCompat.checkSelfPermission(_activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            _isPermissionEnabled = false;
        } else {
            _isPermissionEnabled = true;
        }

        return _instance;
    }

    public Location getLastKnownLocation() {
        if (ActivityCompat.checkSelfPermission(_activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            _isPermissionEnabled = false;
        } else {
            if (_canGetLocation) {
                if (_isNetworkEnabled) {
                    _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (_locationManager != null) {
                        _location = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (_location != null) {
                            _latitude = _location.getLatitude();
                            _longitude = _location.getLongitude();
                        }
                    }
                }

                if (_isGPSEnabled) {
                    if (_location == null) {
                        _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        if (_locationManager != null) {
                            _location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (_location != null) {
                                _latitude = _location.getLatitude();
                                _longitude = _location.getLongitude();
                            }
                        }
                    }
                }
            }
        }

        return _location;
    }

    public void stopUsingGPS() {
        if (_locationManager != null) {
            if (ActivityCompat.checkSelfPermission(_activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                _locationManager.removeUpdates(GPS.this);
            }
        }
    }

    public double getLatitude() {
        if (_locationManager != null) {
            _latitude = _location.getLatitude();
        }

        return _latitude;
    }

    public double getLongitude() {
        if (_locationManager != null) {
            _longitude = _location.getLongitude();
        }

        return _longitude;
    }

    public boolean canGetLocation() {
        return _canGetLocation;
    }

    public boolean isPermissionEnabled() {
        return _isPermissionEnabled;
    }

    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity);
        alertDialog.setTitle("GPS Settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?");

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

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

        alertDialog.show();
    }

    public void requestLocationPermission(LocationPermissionResponseListener listener) {
        _locationPermissionListener = listener;

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

    @Override
    public void onLocationChanged(Location location) {
        this._location = location;
    }

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

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case GPS.LOCATION_REQUEST_CODE: {
                _locationPermissionListener.onResponse(grantResults[0] == PackageManager.PERMISSION_GRANTED);
            }
        }
    }

    public static interface LocationPermissionResponseListener {
        public void onResponse(Boolean permissionGranted);
    }

    }

解决方法 1:

从看出来, GPS 是一个单例类。

有是一个私人的静态字段 _instancetype GPS 也是一个静态的方法,称为 sharedInstance(Activity) 。这些都是单身人士的特点。

一个单例类基本上意味着只会在运行时类的一个实例。在这种情况下,它是 _instance 。它不允许创建其他实例的 GPS 。这就是为什么该构造函数标记为私人性质,使您无法访问它。

因为如果这样做,你不应创建新实例的 GPS 。相反应该通过调用方法来访问只有一个实例 sharedInstance

GPS insgps = GPS.sharedInstance(anActivity);

如果你在写这段代码中的一个子类 Activity ,取代 anActivity 以上与 this 。如果你在写这段代码在一些其他类,获取实例的 Activity 和替换 anActivity 与它。

官方微信
官方QQ群
31647020