How to request permissions during run time when using Gluon mobile environment?

我们两清 提交于 2019-12-06 13:17:11

For starters, you can add the android-support-v4.jar to your project:

Copy it from its location under ANDROID_HOME/extras/android/support/v4/android-support-v4.jar to a libs folder in your project, and then add the dependency to the build.gradle file:

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

Assuming you are targeting Android SDK 23+:

    android {
        minSdkVersion '23'
        compileSdkVersion '23'
        targetSdkVersion '23'
        manifest = 'src/android/AndroidManifest.xml'
    }

then you know that by default all the permissions included in the manifest will be disabled.

If you want to check for permissions on runtime, you can define a new activity that takes care of requesting permissions with a built-in dialog (using ActivityCompat.requestPermissions), register this activity in the manifest, and call it from the FXActivity within a new intent that passes a list with the required permissions.

You just need to call FXActivity.getInstance().setOnActivityResultHandler() to listen to the end of that activity and resume the call if permissions were granted.

The following code is partly based in the PermissionHelper class.

I'll use the simple case of the Dialer service from the new Charm Down 3.0.0 library, that requires Manifest.permission.CALL_PHONE.

AndroidDialerService.java, under Android package

public class AndroidDialerAskService implements DialerAskService {

    public static final String KEY_PERMISSIONS = "permissions";
    public static final String KEY_GRANT_RESULTS = "grantResults";
    public static final String KEY_REQUEST_CODE = "requestCode";

    @Override
    public void call(String number) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                FXActivity.getInstance().setOnActivityResultHandler((requestCode, resultCode, data) -> {
                    if (requestCode == 11112) {
                        // if now we have permission, resume call
                        if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                            call(number);
                        }
                    }
                });

                Intent permIntent = new Intent(FXActivity.getInstance(), PermissionRequestActivity.class);
                permIntent.putExtra(KEY_PERMISSIONS, new String[]{Manifest.permission.CALL_PHONE});
                permIntent.putExtra(KEY_REQUEST_CODE, 11111);
                FXActivity.getInstance().startActivityForResult(permIntent, 11112);
                return;
            }
        }

        if (number != null && !number.isEmpty()) {
            Uri uriNumber = Uri.parse("tel:" + number);
            Intent dial = new Intent(Intent.ACTION_CALL, uriNumber);
            FXActivity.getInstance().startActivity(dial);
        }
    }

    public static class PermissionRequestActivity extends Activity {
        private String[] permissions;
        private int requestCode;

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

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

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

AndroidManifest.xml

. . .
<uses-permission android:name="android.permission.CALL_PHONE"/>
. . .
<activity android:name="javafxports.android.FXActivity" .../>
<activity android:name="com.gluonhq.charm.down.plugins.android.AndroidDialerService$PermissionRequestActivity" />
. . .   

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!