如何授予运行 time 权限 Android API 23 吗?

标签: Android
发布时间: 2016/9/26 3:47:46
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

专家,

下面是我的代码,

问题描述︰ 错误发生在 db.execSQL 声明说空 db 对象。

我想要检查什么是错误的和应用 2 toast ,找出"测试"不显示,我的理解,这意味着 checkSelfPermission 判断是假的所以我有权限已。但如果写了 db = SQLiteDatabase.openOrCreateDatabase() 直接,也将"未知的错误 (代码 14)︰ 无法打开数据库"。

我真的没有任何线索,请帮助,在此先感谢。

ActivityCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED

代码︰

public class AbcActivity extends Activity {

SQLiteDatabase db;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case 111222:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null);
            else {
                System.exit(0);
            }
            break;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_param);

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    111222);
        else
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    111222);
    }
    Toast.makeText(getApplicationContext(), "test2", Toast.LENGTH_SHORT).show();

    //db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null);
    db.execSQL("create table if not exists " + "xxxxx" + "(" +
            //<editor-fold desc="columns">
            "DtdKink REAL," +
            "TttBin REAL" +
            //</editor-fold>
            ")");
}

}

解决方法 1:

我认为它不相关权限,您应该使用 SQLiteOpenHelper 类来使用 SQLite。

示例︰

public class FeedReaderDbHelper extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";

    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

从信息︰

https://developer.android.com/training/basics/data-storage/databases.html

编辑︰

在 API 23 openOrCreateDatabase() 当你尝试在外部存储中创建它,试着做它之前调用此方法时不创建文件夹

请检查此解决方案︰

File root = new File(Global.dataFolder + File.separator);
if (!root.exists()) {
    //Create folder
    root.mkdirs();
}
db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null);
                db.execSQL("create table if not exists " + "xxxxx" + "(" +
                //<editor-fold desc="columns">
                "DtdKink REAL," +
                "TttBin REAL" +
                //</editor-fold>
                ")");
官方微信
官方QQ群
31647020