gpt4 book ai didi

java - 在android studio中调用类函数

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

我正在做一个有 GPS 类的项目

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

像这样

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

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

GPS 在“com.myapp.locationapp.app.GPS”中具有私有(private)访问权限

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

这是我用的类

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

}

最佳答案

从外观上看,GPS 是一个单例类。

有一个GPS 类型的私有(private)静态字段_instance,还有一个名为sharedInstance(Activity) 的静态方法。这些是单例的特征。

单例类基本上意味着该类在运行时只有一个实例。在本例中,它是 _instance。不允许创建 GPS 的其他实例。这就是构造函数被标记为私有(private)的原因,使您无法访问它。

因为如果这样,您不应该创建一个新的 GPS 实例。您应该通过调用方法 sharedInstance 来访问唯一的一个实例。

GPS insgps = GPS.sharedInstance(anActivity);

如果您在 Activity 的子类中编写此代码,请将上面的 anActivity 替换为 this。如果您在其他类中编写此代码,请获取 Activity 的实例并用它替换 anActivity

关于java - 在android studio中调用类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41780337/

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