2

我是新来的,因此我还不知道该网站的所有规则,对此感到抱歉!

我正在使用 Eclipse 工具中的 Gluon 移动插件为桌面和 Android 构建 Java 应用程序。如示例 ( http://docs.gluonhq.com/samples/gonative/ ) 中所述,我为桌面和 Android 提供了不同的代码。所以我有Android特定的NativeService,我有一种从外部存储读取文件的方法(有效)。我还研究了如何在使用原生 Android 环境时请求运行时权限(Marshmallow 中的存储权限错误)。我可以请求许可,但我无法获得结果。

我现在以相同的方式请求权限,但我如何才能收到结果?我不能像示例中那样覆盖 onRequestPermissionsResult,因为我没有直接可用的 android.support.v4.app 东西。有人可以帮助我并提供一个例子吗?

AndroidNativeService.java:

package com.tentaattorix;

import java.io.IOException;

import java.io.File;
import android.os.Environment;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.content.Context;
import javafxports.android.FXActivity;
import android.util.Log;
import android.widget.Toast;
import android.content.pm.PackageManager;
import android.os.Build;
import com.avustajat.LueJaKirjoita;

/**
 * Sanaston luku: Android-natiivi toteutus!
 * @author PT
 * @version 24.10.2016
 *
 */
public class AndroidNativeService implements NativeService {

    private static final String TAG = "Tentaattorix Native Service";
    private Sanastot sanastot = new Sanastot();

    private static final int MY_PERMISSIONS_USE_EXTERNAL_STORAGE = 1;
    public AndroidNativeService() {
        //
    }

    @Override
    public Sanastot haeSanastot(String juuriKansio, String sanastoRegex, char kommentti, char erotin) throws IOException {


        String polku = Environment.getExternalStorageDirectory().toString();
        String readme = "LueMinut.txt";
        String kansioPolku ="";

        //Luodaan kansio, jos sitä ei vielä ole.
        File kansio = new File(polku, juuriKansio);
        kansio.mkdir();

        //Asetetaan oikeudet, jos vaikka auttais skannaukseen.
        kansio.setWritable(true);
        kansio.setReadable(true);
        kansio.setExecutable(true);
        kansioPolku = kansio.getAbsolutePath();


        //Kysy oikeudet, jos niitä ei ole!                                       
        if (isStoragePermissionGranted()) {

            //Luodaan kansioon tiedosto LueMinut.txt.
            try {
                LueJaKirjoita.luoLueMinut(kansioPolku, readme);
            }
            catch (IOException e){
                throw e;
            }

            //Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
            scanFile(kansioPolku + File.separator + readme);


            //Luetaan sanastot kansiosta.
            sanastot = LueJaKirjoita.lueTiedostot(kansioPolku, sanastoRegex, kommentti, erotin);

        }

        // Jos sanastot ei sisällä yhtään sanastoa, 
        // niin laitetaan edes yksi :) 
        if (sanastot.annaLkm() < 1) {
            String[] rivix = {"Tyhjä sanasto!", "Empty glossary!"};
            Sanasto san = new Sanasto("sanasto_");
            san.lisaa(rivix);
            sanastot.lisaa(san);
        }
        return sanastot;
    }

    /**
     * //Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
     * @param path lisätty polku+tiedosto
     */
    private void scanFile(String path) {

        MediaScannerConnection.scanFile(FXActivity.getInstance().getApplicationContext(),
                new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

            public void onScanCompleted(String path, Uri uri) {
                Log.i("TAG", "Finished scanning " + path);
            }
        });
    }

    private  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (FXActivity.getInstance().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v(TAG,"Permission is granted");
                return true;
            } else {

                Log.v(TAG,"Permission is revoked");
                FXActivity.getInstance().requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_USE_EXTERNAL_STORAGE);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            Log.v(TAG,"Permission is granted");
            return true;
        }
    }
} 
4

1 回答 1

3

对于初学者,您可以将其添加android-support-v4.jar到您的项目中:

将其从 ANDROID_HOME/extras/android/support/v4/android-support-v4.jar 下的位置复制到libs项目中的文件夹,然后将依赖项添加到 build.gradle 文件中:

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

假设您的目标是 Android SDK 23+:

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

那么您知道默认情况下清单中包含的所有权限都将被禁用。

如果您想在运行时检查权限,您可以定义一个新的活动来处理使用内置对话框(使用ActivityCompat.requestPermissions)请求权限,在清单中注册此活动,并从通过FXActivity的新意图中调用它具有所需权限的列表。

您只需要打电话FXActivity.getInstance().setOnActivityResultHandler()来收听该活动的结束并在获得权限的情况下恢复通话。

以下代码部分基于PermissionHelper 类

我将使用来自新 Charm Down 3.0.0的 Dialer 服务的简单案例,它需要Manifest.permission.CALL_PHONE.

AndroidDialerService.java,在Android包下

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" />
. . .   

于 2016-11-01T23:16:26.753 回答