我正在尝试将askForPermisions
函数实现为AIR的Adobe Native Extension(ANE)。我的功能现在定义为:
public class APKaskForPermission implements FREFunction {
public static final String TAG = "askForPermission";
@Override
public FREObject call(FREContext ctx, FREObject[] passedArgs) {
FREObject result = null;
String permission;
Integer requestCode;
try{
permission = passedArgs[0].getAsString();
requestCode = passedArgs[1].getAsInt();
if (ContextCompat.checkSelfPermission(NativeExtension.appContext, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(ctx.getActivity(), permission)) {
Log.d(TAG, "shouldShowRequestPermissionRationale: " + permission);
//This is called if user has denied the permission before
//In this case I am just asking the permission again
Log.d(TAG, "requestPermissions: " + permission);
ActivityCompat.requestPermissions(ctx.getActivity(), new String[]{permission}, requestCode);
} else {
Log.d(TAG, "requestPermissions: " + permission);
ActivityCompat.requestPermissions(ctx.getActivity(), new String[]{permission}, requestCode);
}
result = FREObject.newObject(false);
} else {
Log.d(TAG, "" + permission + " is already granted.");
//Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
result = FREObject.newObject(true);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
}
这会按预期显示对话框,但我无法找到如何获取响应,因为Web上的所有示例都在MainActivity源中定义它,但我在本机扩展上下文中。
有任何帮助吗?
答案 0 :(得分:0)
正如<{3}}中提到的 Wadedwalker ,无法在运行时覆盖或添加方法到另一个类,在本例中为主AIR应用程序活动。因此,最好的方法是启动一个新的空白活动,以便触发弹出窗口,以便能够等待onRequestPermissionsResult()响应。
我设法获得了自己的基本ANE版本,这是在这里开源的:this discussion