1

My Application wants to Request for Wifi Permission on the start.So far I have managed to give permission through Setting.But I don't want to Do that anymore.So far I have seen this previous Question.I could understand a bit.but it's not Enough. I created a .class that that manages Wifi Activity like turning ON/OFF , checking wifi state.It works well if Wifi permission is granted.but if not it does'nt work.
Here's the WifiController.class that I have created.

import android.content.Context;
import android.net.wifi.WifiManager;
import javafxports.android.FXActivity;


public class WifiController implements WifiInterface
{

WifiManager wifiManager=null;

WifiController()
{
    wifiManager = (WifiManager)FXActivity.getInstance().getSystemService(Context.WIFI_SERVICE);
}

WifiManager getWifimanager()
{
    return wifiManager;
}

public boolean isTurnedOn()
{
    return wifiManager.isWifiEnabled();
}

public boolean turnOn()
{
    wifiManager.setWifiEnabled(true);
    return true;
}

public boolean turnOff()
{
    wifiManager.setWifiEnabled(false);
    return true;
}

}

So, Please Enlighten Me. Thanks in Advance.

guru_007
  • 473
  • 4
  • 16

3 Answers3

2

The Android Developers Website has a great walkthrough example on how runtime permissions work here:

https://developer.android.com/training/permissions/requesting.html

Also don't forget to add the permissions into your manifest as well.

cjnash
  • 1,228
  • 3
  • 19
  • 37
2

You have ask for the permission at run time:

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {


if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_EXTERNAL_STORAGE)) {

    // Show an explanation 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(thisActivity,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            REQUEST_PERMISSION);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}

and than do what ever you want (if the user grand the permission):

@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Permission granted.
    } else {
        // User refused to grant permission.
    }
}
}

You can read more here: https://developer.android.com/training/permissions/requesting.html

1

Heavily based on this answer, I created the following class to request permissions on the app start when android version >= Marshmallow:

public class AndroidPlatform {

    private static final String KEY_PERMISSIONS              = "permissions";
    private static final String KEY_REQUEST_CODE             = "requestCode";
    private static final int    REQUEST_CODE_ASK_PERMISSIONS = 246;
    private static final int    REQUEST_CODE                 = 123;

    private final FXActivity    activity;

    public AndroidPlatform() {
        activity = FXActivity.getInstance();
    }

    public void checkPermissions() {
        if (Build.VERSION.SDK_INT >= MARSHMALLOW) {

            List<String> requiredPermissions = new ArrayList<>();
            addPermission(activity, requiredPermissions , Manifest.permission.ACCESS_COARSE_LOCATION);
            // additional required permissions

            if (!requiredPermissions.isEmpty()) {
                Intent permIntent = new Intent(activity, PermissionRequestActivity.class);
                permIntent.putExtra(KEY_PERMISSIONS, requiredPermissions.toArray(new String[requiredPermissions .size()]));
                permIntent.putExtra(KEY_REQUEST_CODE, REQUEST_CODE);
                activity.startActivityForResult(permIntent, REQUEST_CODE_ASK_PERMISSIONS);
            }
        }
    }

    private void addPermission(Activity activity, List<String> permissionsList, String permission) {
        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
        }
    }

    public static class PermissionRequestActivity extends Activity {

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            FXActivity.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
            finish();
        }

        @Override
        protected void onStart() {
            super.onStart();
            String[] permissions = this.getIntent().getStringArrayExtra(KEY_PERMISSIONS);
            int requestCode = this.getIntent().getIntExtra(KEY_REQUEST_CODE, 0);

            ActivityCompat.requestPermissions(this, permissions, requestCode);
        }
    }

}

In your AndroidManifest.xml you have to add the corresponding activity:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<activity android:name="yourpackage.AndroidPlatform$PermissionRequestActivity" />

Also you have to add the android-support-library to your build.gradle:

dependencies {
    androidCompile files('libs/android-support-v4.jar')
    compileNoRetrolambda files('libs/android-support-v4.jar')
}

For further information regarding wifiManager / permissions see link

jns
  • 6,017
  • 2
  • 23
  • 28
  • \AndroidPlatform.java:60: error: cannot find symbol FXActivity.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults); ^ symbol: method onRequestPermissionsResult(int,String[],int[]) location: variable act of type FXActivity – guru_007 Jul 12 '17 at 16:25
  • I am getting this above error @jns ,I have included all the libraries required and my ide shows it's Error free but on launching the task **android install** am getting this above error. – guru_007 Jul 12 '17 at 16:30
  • That API requires Android SDK 23+. You need to set the `compileSdkVersion` parameter accordingly. – José Pereda Jul 13 '17 at 09:51
  • 1
    your code works for Location services and other services ! but it's not working as expected for **CHANGE_WIFI_STATE** permission ,Consider a situation where Initially the permission is granted when installing ,but after installation I removed the this permisssion through setting ,when I call **checkPermissions()**,this code considers that **CHANGE_WIFI_STATE** is still given.I want to ask permission on run-time on checking whether it has the permission or Not. – guru_007 Jul 18 '17 at 13:56
  • To check if a permission is granted you can use: `ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED)` – jns Jul 18 '17 at 16:26