如何使用Android中数据库的运行时间权限



我对数据库的运行时许可有问题。我不知道如何使用和编写代码的使用权限。我非常搜索,但在迷失方面理解。代码是:

public class G  {
public static Context        context;
public static SQLiteDatabase database;
public static final String   DIR_SDCARD   = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String   DIR_DATABASE = DIR_SDCARD + "/database-test";
@Override
public void onCreate() {
    super.onCreate();
    context = this.getApplicationContext();
   // new File(DIR_DATABASE).mkdirs();
    File file=new File(DIR_DATABASE);
    file.mkdirs();
    database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/database.sqlite", null);
    database.execSQL("CREATE  TABLE  IF NOT EXISTS person (person_name TEXT NOT NULL ," +
                " person_family TEXT NOT NULL , " +
                " person_password TEXT NOT NULL   )");
}

}请帮助我

您不需要在Android中自己数据库的Android运行时权限。您要做的就是创建数据库并对它进行一些CRUD操作。

数据库位于应用程序分配的空间内部,因此不需要任何权限。但是,如果您要读/写入SDCARD,则可能需要这些运行时间权限。

您可以在这里找到详细的文档,

https://developer.android.com/training/permissions/requesting.html

这是在运行时请求权限的非常好的指南。基本上,检查和征求许可的代码是:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

然后,您将获得结果:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay! Do the
                // contacts-related task you need to do.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

最新更新