-2

I am getting this error after i am try to updated my compileSdkVersion andTargetSdkversion greater than 22. Seems like I am doing any GUI task in my doInBackground method but i am unable to figure it out. I would be much thankful for a solution.

This is my stack trace:

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) 

Method in class WlanInfoAdapter.java:72 is here:

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

Method in changeableArrayAdapter.java class showing error is given below. its where getNewItems() method is called: 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;
            }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

You need to add ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION as permissions to your Android manifest file.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
Tobias G
  • 583
  • 2
  • 10
0

If you are targeting API 22 only then you need to add these 2 permission in your android manifest file

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  

And if you are targeting for Android Marshmallow(Api 23 and above) you need to give runtime permission in your app.

swetabh suman
  • 1,949
  • 2
  • 19
  • 24
  • thanks for your answer. I already added that two permissions in my manifest file. Still my app is crashing . I think i will try giving runtime permission in my app. i would like to ask is this what you are talking about https://developer.android.com/training/permissions/requesting.html , if not please provide me the right link if its possible. would be much thankful – LanguageMaster Feb 17 '17 at 08:38
  • Yes this only. Or you can google it on how to add runtime permission of coarse location in android – swetabh suman Feb 17 '17 at 08:59
0

Add these to your AndroidManifest.xml before application tag

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Also for Android version >= 23, you need to request permissions in run-time

     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
     }

And

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.
        }
    }

Also

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

And

   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;
    }
Atef Hares
  • 4,715
  • 3
  • 29
  • 61