gpt4 book ai didi

android - 如何允许应用程序访问设备的位置

转载 作者:行者123 更新时间:2023-11-29 02:39:09 24 4
gpt4 key购买 nike

我已经创建了可以访问您的位置的程序,为此我需要获得允许访问我的位置的权限。我知道要申请的许可以及如何处理该许可,但我无法在我的主程序中完成。每次我尝试添加权限时它都不会读取 requestPermission() ,为了让它读取我也添加了 @RequiresApi(api = Build.VERSION_CODES.M) .谁能告诉我如何在我的代码中添加它。

我有两个类:1) NetwrokProviderActivity,我在其中调用我的 2) 类 ApplocationService。在这里我分享我的代码。请帮我解决这个问题

我需要的权限

if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
},10);
return;

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode)
{
case 10:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
configureButton();
return;
}
}
}

我需要在其中添加此权限的 ApplocationClass

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

public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isNetworkEnabled) {

} else {
this.canGetLocation = true;

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

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

NetwrokprovideActivity

AppLocationService appLocationService;
Button btnNetworkProvider;
TextView txtLati;
TextView txtLongi;
TextView txtAddress;
TextView txtCity;
TextView txtState;
TextView txtCountry;
TextView txtPostalCode;
protected Context context=this;
Location location;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;


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


txtLati= (TextView) findViewById(R.id.txt_Latitude);
txtLongi= (TextView) findViewById(R.id.txt_Longitude);
txtAddress= (TextView) findViewById(R.id.txt_NetAddress);
txtCity= (TextView) findViewById(R.id.txt_NetCity);
txtState= (TextView) findViewById(R.id.txt_NetState);
txtCountry= (TextView) findViewById(R.id.txt_NetCountry);
txtPostalCode= (TextView) findViewById(R.id.txt_NetPostalCode);

appLocationService = new AppLocationService(NetworkProviderActivity.this);


btnNetworkProvider = (Button) findViewById(R.id.btn_Location_NetworkProvider);
btnNetworkProvider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

Location nwLocation = appLocationService
.getLocation();

if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();

List<Address> addresses;
Geocoder geocoder=new Geocoder(context, Locale.getDefault());

txtLati.setText("Latitude: "+latitude);
txtLongi.setText("Longitude: "+ longitude);

try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();

Log.e(TAG,"network...");

txtAddress.setText("Address: "+address);
txtCity.setText("City: "+city);
txtState.setText("State: "+state);
txtCountry.setText("Country: "+country);
txtPostalCode.setText("Postal Code: "+postalCode);
}


} catch (IOException e) {
e.printStackTrace();
}


Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
} else {
showSettingsAlert("NETWORK");
}

}
});
}


public void showSettingsAlert(String provider) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
NetworkProviderActivity.this);

alertDialog.setTitle(provider + " SETTINGS");

alertDialog
.setMessage(provider + " is not enabled! 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);
NetworkProviderActivity.this.startActivity(intent);
}
});

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

alertDialog.show();
}

最佳答案

您需要在 list 上添加权限,以支持等于和高于 23 的 API,您还必须以编程方式请求。

关于android - 如何允许应用程序访问设备的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45408245/

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