致命异常:java.lang.SecurityException:需要访问\u粗略\u位置或访问\u精细\u位置权限
在尝试更新大于22的CompileSDK版本和TargetSDK版本后,出现此错误。似乎我在我的doInBackground方法中执行任何GUI任务,但我无法理解它。如果能找到解决办法,我将不胜感激 这是我的堆栈跟踪:致命异常:java.lang.SecurityException:需要访问\u粗略\u位置或访问\u精细\u位置权限,java,android,android-asynctask,android-permissions,runtimeexception,Java,Android,Android Asynctask,Android Permissions,Runtimeexception,在尝试更新大于22的CompileSDK版本和TargetSDK版本后,出现此错误。似乎我在我的doInBackground方法中执行任何GUI任务,但我无法理解它。如果能找到解决办法,我将不胜感激 这是我的堆栈跟踪: E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: xy.abc.xyz.xy, PID: 15246 java.lang.RuntimeExce
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: xy.abc.xyz.xy, PID: 15246
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.net.wifi.IWifiManager$Stub$Proxy.getScanResults(IWifiManager.java:1066) at android.net.wifi.WifiManager.getScanResults(WifiManager.java:1311)
at xy.abc.xyz.xy.config.WlanInfoAdapter.getNewItems(WlanInfoAdapter.java:72)
at xy.abc.xyz.xy.android.data.ChangeableArrayAdapter$1.doInBackground(ChangeableArrayAdapter.java:78)
at xy.abc.xyz.xy.android.data.ChangeableArrayAdapter$1.doInBackground(ChangeableArrayAdapter.java:74)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
类WlanInfoAdapter.java:72中的方法如下:
@Override
protected List<WlanInfo> getNewItems() {
WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResults = wifiManager.getScanResults();
List<WlanInfo> infoList = new ArrayList<>(scanResults.size());
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
try {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
Map<String, WifiConfiguration> configuredNetworkIds = new HashMap<>();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (configuredNetwork.SSID != null) {
configuredNetworkIds.put(WlanInfo.cleanSSID(configuredNetwork.SSID), configuredNetwork);
} else {
wifiManager.removeNetwork(configuredNetwork.networkId);
}
}
for (ScanResult scanResult : scanResults) {
WlanInfo wlanInfo = new WlanInfo(scanResult);
WifiConfiguration configuration = configuredNetworkIds.get(wlanInfo.getSSID());
wlanInfo.setConfiguration(configuration);
wlanInfo.setIsActive(configuration != null && configuration.networkId == connectionInfo.getNetworkId());
if (showUnknown || wlanInfo.isConfigured() || wlanInfo.isOpen()) {
infoList.add(wlanInfo);
}
}
}
catch (Exception e) { e.printStackTrace(); return null; }
return infoList;
}
@覆盖
受保护的列表getNewItems(){
WifiManager WifiManager=(WifiManager)getContext().getSystemService(Context.WIFI_服务);
List scanResults=wifiManager.getScanResults();
List infoList=newarraylist(scanResults.size());
WifiInfo connectionInfo=wifiManager.getConnectionInfo();
试一试{
List configuredNetworks=wifiManager.getConfiguredNetworks();
Map ConfiguredNetworkId=新HashMap();
用于(无线配置配置数据网络:配置数据网络){
if(configuredNetwork.SSID!=null){
configuredNetworkId.put(WlanInfo.CleansId(configuredNetwork.SSID),configuredNetwork);
}否则{
wifiManager.removeNetwork(configuredNetwork.networkId);
}
}
用于(扫描结果扫描结果:扫描结果){
WlanInfo WlanInfo=新WlanInfo(扫描结果);
WifiConfiguration configuration=configuredNetworkIds.get(wlanInfo.getSSID());
wlanInfo.setConfiguration(配置);
wlanInfo.setIsActive(配置!=null&&configuration.networkId==connectionInfo.getNetworkId());
if(showUnknown | | wlanInfo.isConfigured()| | wlanInfo.isOpen()){
infoList.add(wlanInfo);
}
}
}
catch(异常e){e.printStackTrace();返回null;}
返回信息列表;
}
下面给出了changeableArrayAdapter.java类中显示错误的方法。在其中调用getNewItems()方法:
ChangeableArrayAdapter.java:78
ChangeableArrayAdapter.java:74
public void triggerRefresh() {
AsyncTask<Void, Void, List<T>> asyncTask = new AsyncTask<Void, Void, List<T>>() {
@Override
protected List<T> doInBackground(Void... objects) {
return getNewItems();
//return null;
}
public void触发器刷新(){
AsyncTask AsyncTask=新建AsyncTask(){
@凌驾
受保护列表doInBackground(无效…对象){
返回getNewItems();
//返回null;
}
您需要将访问粗略位置
或访问精细位置
添加为您的Android清单文件的权限。如果您仅针对API 22,则需要将这2个权限添加到您的Android清单文件中
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
如果您的目标是Android棉花糖(Api 23及以上)您需要在应用程序中授予运行时权限。将这些添加到
AndroidManifest.xml
之前的应用程序
标记中
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
及
也
及
public static boolean ispermissiongrantpermissions(@NonNull String[]grantPermissions,@NonNull int[]grantResults,
@非空字符串权限){
for(int i=0;i
谢谢你的回答。我已经在清单文件中添加了这两个权限。但我的应用程序仍在崩溃。我想我会尝试在我的应用程序中授予运行时权限。我想问一下这就是你所说的,如果不是,请尽可能提供正确的链接。非常感谢是的,仅此而已。或者你可以在谷歌上搜索它如何在Android中添加粗略位置的运行时权限谢谢你的全面解释。我想知道我是否应该将位置(u permission)请求(u REQUEST)编码为最终staic变量并将其值设置为1???@LanguageMaster是的,这正是它的含义。你真是太棒了,曼恩。你让我开心了。非常感谢你的回答。这是工作时间。
if (ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
askForLocationPermissions();
} else {
//do your work
}
private void askForLocationPermissions() {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("Location permessions needed")
.setMessage("you need to allow this permission!")
.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(YourActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// //Do nothing
}
})
.show();
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
//Do you work
} else {
Toast.makeText(this, "Can not proceed! i need permission" , Toast.LENGTH_SHORT).show();
}
break;
}
}
public static boolean isPermissionGranted(@NonNull String[] grantPermissions, @NonNull int[] grantResults,
@NonNull String permission) {
for (int i = 0; i < grantPermissions.length; i++) {
if (permission.equals(grantPermissions[i])) {
return grantResults[i] == PackageManager.PERMISSION_GRANTED;
}
}
return false;
}