2017-06-06 76 views
-1

我有数据库的运行时权限问题。我不知道如何使用和写入我的代码的权限。我非常寻求,但迷失了解。 代码:如何在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 )"); 
} 

} 请帮我

+0

https://stackoverflow.com/a/6168988/2459628 – IvBaranov

+0

(HTTPS [创建数据库的权限的Android]的可能重复:// stackoverflow.com/questions/6165887/android-permission-of-creating-database) –

回答

0

Here是一个很好的指南要求的权限在运行时。基本上,用于检查和询问许可的代码是:

// 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 
    } 
} 
1

你不需要的Android运行时的权限在自己的数据库Android系统。你所要做的就是创建数据库并对其执行一些CRUD操作。

数据库驻留在应用程序的分配空间内,因此不需要任何权限。但是,如果您要读取/写入SD卡,那么您可能需要这些运行时权限。

你可以找到一个详细的文件就在这里,

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

相关问题