我是新来的,并且有理由说我还不知道该网站的所有规则,对不起!
我正在使用Eclipse工具中的Gluon移动插件为Desktop和Android构建Java应用程序。我在桌面和Android中有变量代码,如示例(http://docs.gluonhq.com/samples/gonative/)中所述。所以我有Android特定的NativeService,我有一个从外部存储读取文件的方法(可行)。我还研究了在使用原生Android环境(Storage permission error in Marshmallow)时如何请求运行时权限。我可以请求许可,但我无法理解结果。
我现在以相同的方式请求权限,但我如何才能收到结果?我不能在示例中覆盖onRequestPermissionsResult,因为我没有直接使用android.support.v4.app的东西。有人可以帮助我并提供一个例子吗?
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;
}
}
}
答案 0 :(得分: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 class。
我将使用新Charm Down 3.0.0 library中的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" />
. . .