简体   繁体   English

Qt 最近(>6)台设备未授予应用程序权限 Android 设备

[英]Qt app permissions not granted on recent (>6) Android devices

Using Qt 5.14.2, with NDK r21b, I create this simple program:使用 Qt 5.14.2 和 NDK r21b,我创建了这个简单的程序:

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QDir>
#include <QStandardPaths>
#include <QFile>
#include <QTextStream>
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog dlg;

    dlg.setLayout( new QVBoxLayout() );

    QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

    if ( !QDir(path).exists() )
    {
        if ( QDir().mkdir(path) )
            dlg.layout()->addWidget( new QLabel( "Could create folder " + path, &dlg ) );
        else
            dlg.layout()->addWidget( new QLabel( "Could NOT create folder " + path, &dlg ) );
    }
    else
    {
        dlg.layout()->addWidget( new QLabel( "Folder exists " + path, &dlg ) );
    }

    path += "/from_qt.txt";

    QFile file(path);
    if (file.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&file);
        stream << "something" << endl;
        dlg.layout()->addWidget( new QLabel( "Could create file " + path, &dlg ) );
    }
    else
    {
        dlg.layout()->addWidget( new QLabel( "Could NOT create file " + path, &dlg ) );
    }

    dlg.show();
    return a.exec();
}

I add android.permission.WRITE_EXTERNAL_STORAGE to AndroidManifest.xml file.我将android.permission.WRITE_EXTERNAL_STORAGE添加到AndroidManifest.xml文件。

  • Using Android 5, when installing the apk, the system reports the app needs to access storage and later the app will work (it reports Could NOT create... ).使用Android 5、安装apk时,系统提示app需要访问存储,稍后app可以运行(提示Could NOT create... )。
  • Using Android 6, when installing the apk, the system does not report the app needs any special access and later the app will work.使用 Android 6、安装apk时,系统没有提示app需要任何特殊权限,稍后app就可以运行了。

According to https://developer.android.com/training/permissions/usage-notes :根据https://developer.android.com/training/permissions/usage-notes

Beginning with Android 6.0 (API level 23), users grant and revoke app permissions at run time, instead of doing so when they install the app.从 Android 6.0(API 级别 23)开始,用户可以在运行时授予和撤销应用程序权限,而不是在安装应用程序时这样做。 As a result, you'll have to test your app under a wider range of conditions.因此,您必须在更广泛的条件下测试您的应用。 Prior to Android 6.0, you could reasonably assume that if your app is running at all, it has all the permissions it declares in the app manifest.在 Android 6.0 之前,您可以合理地假设如果您的应用程序正在运行,它具有它在应用程序清单中声明的所有权限。 Beginning with Android 6.0 the user can turn permissions on or off for any app, even an app that targets API level 22 or lower.从 Android 6.0 开始,用户可以打开或关闭任何应用程序的权限,即使是针对 API 级别 22 或更低级别的应用程序。 You should test to ensure your app functions correctly whether or not it has any permissions.您应该测试以确保您的应用程序正常运行,无论它是否具有任何权限。

So you need to add QT += androidextras in.pro file, include <QtAndroid> and add in the code:所以你需要在.pro文件中添加QT += androidextras ,包含<QtAndroid>并在代码中添加:

auto  result = QtAndroid::checkPermission(QString("android.permission.WRITE_EXTERNAL_STORAGE"));
        if(result == QtAndroid::PermissionResult::Denied){
            QtAndroid::PermissionResultMap resultHash = QtAndroid::requestPermissionsSync(QStringList({"android.permission.WRITE_EXTERNAL_STORAGE"}));
            if(resultHash["android.permission.WRITE_EXTERNAL_STORAGE"] == QtAndroid::PermissionResult::Denied)
                return 1;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM