Android:位置服务权限
Posted
技术标签:
【中文标题】Android:位置服务权限【英文标题】:Android: Location Service permission 【发布时间】:2017-09-20 21:47:22 【问题描述】:我从位置管理器访问位置时遇到问题
这是错误
04-24 11:12:10.999 9378-9378/com.example.devandrin.myapplication D/HomeActivity: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
定位函数代码
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean GPS;
try
GPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
catch (Exception e)
GPS = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
boolean data = info != null;
if (!data || !GPS)
Utilities.MakeSnack(findViewById(R.id.cLayout), "Unable to get location");
else
try
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1200, 50, new LocationListener()
@Override
public void onLocationChanged(Location location)
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
@Override
public void onProviderDisabled(String provider)
);
Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String url = "/setuserlocation/" + sp.getString("userID", "") +
"/" + l.getLatitude() +
"/" + l.getLongitude();
StringRequest sr = new StringRequest(StringRequest.Method.GET, url, new Response.Listener<String>()
@Override
public void onResponse(String response)
Utilities.MakeToast(getInstance(), "Location Updated");
GregorianCalendar gc = new GregorianCalendar();
Date date = gc.getTime();
SharedPreferences.Editor e = sp.edit();
e.putString("DatePosted", date.toString());
e.commit();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Utilities.MakeToast(getInstance(), "Location Wasn't updated");
);
RequestQueueSingleton.getInstance(this).getRequestQueue().add(sr);
catch (SecurityException e)
Log.d("HomeActivity", e.getMessage());
以及清单中设置的权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
应用程序的 api 级别是 16,因此我无法在运行时请求权限,并且应用程序不会在安装时请求权限,谁能帮我解决这个问题....
【问题讨论】:
确保权限不在清单中的应用程序标记中,如果可能,请粘贴整个清单 你的 targetSdkVersion 是什么? @AdeelTurk 这是正确的,因为 Internet 权限正在工作,我有一个 webview,我确定它不是 【参考方案1】:像这样获得你的运行时权限
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
// Permission already Granted
//Do your work here
//Perform operations here only which requires permission
else
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, 1);
如果尚未授予权限,则覆盖这样的 onRequestPermission 结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
if (requestCode == 1)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
//Permission Granted
//Do your work here
//Perform operations here only which requires permission
【讨论】:
当我允许权限时它会崩溃,但是当我重新打开时它会正常工作 只有在获得权限时才执行操作。否则会崩溃。将代码放入此条件和 requestPermissionResult 中以上是关于Android:位置服务权限的主要内容,如果未能解决你的问题,请参考以下文章
即使应用程序在android中关闭,如何在服务应该工作时添加位置权限?