Android 如何允许应用程序访问设备';s位置
我已经创建了一个程序,可以访问您的位置,为此,我需要获得允许访问我的位置的权限。我知道要申请的权限以及如何处理该权限,但我无法在我的主程序中完成。每次我尝试添加权限时,它都不会读取requestPermission(),为了使其读取,我也添加了@RequiresApi(api=Build.VERSION\u CODES.M)这个。有人能告诉我如何在我的代码中添加它吗。Android 如何允许应用程序访问设备';s位置,android,geolocation,location,android-permissions,Android,Geolocation,Location,Android Permissions,我已经创建了一个程序,可以访问您的位置,为此,我需要获得允许访问我的位置的权限。我知道要申请的权限以及如何处理该权限,但我无法在我的主程序中完成。每次我尝试添加权限时,它都不会读取requestPermission(),为了使其读取,我也添加了@RequiresApi(api=Build.VERSION\u CODES.M)这个。有人能告诉我如何在我的代码中添加它吗。 我有两个类:1)netwrokprovider活动,我在其中调用我的2)类ApplocationService。我在这里分享我的
我有两个类:1)netwrokprovider活动,我在其中调用我的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;
}
netwrokprovidedactivity
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();
}
AppLocationService AppLocationService;
按钮btnNetworkProvider;
TextView-txtLati;
TextView-txtLongi;
TextView txtAddress;
TextView-txtCity;
TextView txtState;
TextView txtCountry;
TextView-txtPostalCode;
受保护的上下文=此;
位置;
位置请求mLocationRequest;
GoogleapClient MGoogleapClient;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u network\u provider);
txtLati=(TextView)findViewById(R.id.txt_纬度);
txtLongi=(TextView)findViewById(R.id.txt_经度);
txtAddress=(TextView)findViewById(R.id.txt\u NetAddress);
txtCity=(TextView)findViewById(R.id.txt\u NetCity);
txtState=(TextView)findViewById(R.id.txt\u NetState);
txtCountry=(TextView)findViewById(R.id.txt\u NetCountry);
txtPostalCode=(TextView)findViewById(R.id.txt\u NetPostalCode);
appLocationService=新的appLocationService(NetworkProviderActivity.this);
btnNetworkProvider=(按钮)findViewById(R.id.btn\u Location\u NetworkProvider);
btnNetworkProvider.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
位置nwLocation=appLocationService
.getLocation();
如果(nwLocation!=null){
双纬度=nwLocation.getLatitude();
double longitude=nwLocation.getLongitude();
列出地址;
Geocoder Geocoder=新的地理编码器(上下文,Locale.getDefault());
setText(“纬度:”+纬度);
txtLongi.setText(“经度:+经度”);
试一试{
地址=地理编码器.getFromLocation(纬度,经度,1);
if(addresses!=null&&addresses.size()>0){
字符串地址=地址.get(0).getAddressLine(0);
字符串city=addresses.get(0.getLocation();
字符串状态=addresses.get(0.getAdminArea();
字符串country=addresses.get(0.getCountryName();
字符串postalCode=addresses.get(0.getPostalCode();
字符串knownName=addresses.get(0.getFeatureName();
Log.e(标签“网络…”);
setText(“地址:”+地址);
setText(“城市:+City”);
setText(“State:+State”);
setText(“国家:+国家”);
setText(“邮政编码:“+postalCode”);
}
}捕获(IOE异常){
e、 printStackTrace();
}
Toast.makeText(
getApplicationContext(),
移动位置(西北):\n纬度:“+纬度”
+“\n经度:”+经度,
Toast.LENGTH_LONG).show();
}否则{
showSettingsAlert(“网络”);
}
}
});
}
public void showSettingsAlert(字符串提供程序){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(
NetworkProviderActivity.this);
alertDialog.setTitle(提供程序+“设置”);
警报对话框
.setMessage(提供程序+“未启用!要转到设置菜单吗?”);
alertDialog.setPositiveButton(“设置”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
意图=新意图(
设置。操作(位置(源设置);
NetworkProviderActivity.this.startActivity(意向);
}
});
alertDialog.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
alertDialog.show();
}
您需要添加清单上的权限,要支持等于或高于23的API,您还必须通过编程进行请求。您需要在maniufest中添加此权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
最好使用FusedLocationProviderAPI
。它只提供纬度和经度。那么你需要什么?我需要完整地址。@Piyus谢谢你的建议。我已经尝试了FusedLocationProviderAPI。但是,我想要的完整地址是街道号,本地地址。我也添加了。我已经在mainfest中添加了。你能告诉我在我的代码(类:ApplocationService)中在哪里添加权限吗。因为我无法读取我的requestpermission()权限方法。如果(isReadStorageAllowed()){添加你的谷歌地图活动}其他{requestStoragePermission();}私有布尔值isReadStorageAllowed(){//获取权限状态int result=Cont