日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android入门(13)| Android权限 与 内容提供器

發(fā)布時(shí)間:2023/12/13 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android入门(13)| Android权限 与 内容提供器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 普通權(quán)限與危險(xiǎn)權(quán)限
    • 運(yùn)行時(shí)申請權(quán)限
  • 內(nèi)容提供器
    • 運(yùn)用安卓封裝好的內(nèi)容提供器
    • 自實(shí)現(xiàn)的內(nèi)容提供器
      • 概念
      • 實(shí)現(xiàn)


普通權(quán)限與危險(xiǎn)權(quán)限

主要用于不同應(yīng)用程序之間在保證被訪數(shù)據(jù)的安全性的基礎(chǔ)上,實(shí)現(xiàn)數(shù)據(jù)共享的功能

在 Android 6.0 開始引入了運(yùn)行時(shí)權(quán)限的功能,用戶在安裝軟件時(shí)不需要一次性授權(quán)所有的權(quán)限,而是在軟件的使用過程中再對某一項(xiàng)權(quán)限進(jìn)行申請。Android 將權(quán)限分為兩類:

  • 普通權(quán)限: 不會直接影響到用戶的安全和隱私的權(quán)限,對于這部分權(quán)限,系統(tǒng)自動授權(quán)。
  • 危險(xiǎn)權(quán)限: 可能會涉及到用戶的隱私或者對設(shè)備安全性造成影響的權(quán)限。

危險(xiǎn)權(quán)限如下,這些權(quán)限需要進(jìn)行運(yùn)行時(shí)權(quán)限處理,不在表中的權(quán)限只需要在 AndroidManifest.xml 添加權(quán)限聲明即可:

表中的每一個(gè)危險(xiǎn)權(quán)限都屬于一個(gè)權(quán)限組,雖然在進(jìn)行權(quán)限處理的時(shí)候使用的是權(quán)限名,但是一旦用戶同意授權(quán),那么該權(quán)限名對應(yīng)的權(quán)限組中的所有權(quán)限也會同時(shí)被授權(quán)

運(yùn)行時(shí)申請權(quán)限

給按鈕注冊點(diǎn)擊事件:

Button button1 = findViewById(R.id.button_1);button1.setOnClickListener((View view)->{try {/*// 打開撥號界面,無需聲明權(quán)限Intent intent = new Intent(Intent.ACTION_DIAL);*/// 打電話,需要生命權(quán)限Intent intent = new Intent(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:15309276440"));startActivity(intent);} catch (SecurityException e){e.printStackTrace();}});

在注冊表中加入:

這樣的程序在 Android 6.0 之前都可以正常運(yùn)行,但是在更高的版本點(diǎn)擊按鈕后沒有任何效果,錯(cuò)誤信息如下:

權(quán)限被禁止。

修復(fù)這個(gè)問題,申請運(yùn)行時(shí)權(quán)限的流程:

將打電話的行為封裝成函數(shù) call():

private void call(){try {/*// 打開撥號界面,無需聲明權(quán)限Intent intent = new Intent(Intent.ACTION_DIAL);*/// 打電話,需要聲明權(quán)限Intent intent = new Intent(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:15309276440"));startActivity(intent);} catch (SecurityException e){e.printStackTrace();}}

修改 onCreate 方法內(nèi)的點(diǎn)擊按鈕行為:

Button button1 = findViewById(R.id.button_1);button1.setOnClickListener((View view)->{// 相等說明用戶已授權(quán),不等說明未授權(quán)if(ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){// 申請授權(quán)ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.CALL_PHONE}, 1);} else {call();}});
  • 通過 ContextCompat.checkSelfPermission() 方法檢測用戶是否已授權(quán),該方法有兩個(gè)參數(shù):
  • context
  • 具體權(quán)限名
  • 未授權(quán)則需要調(diào)用 ActivityCompat.requestPermissions() 方法來向用戶申請授權(quán),該方法接受三個(gè)參數(shù):
  • Activity 實(shí)例,也就是當(dāng)前活動。
  • String 數(shù)組,也就是要申請的權(quán)限名。
  • 請求碼,必須唯一,這里傳入 1。
  • 調(diào)用 requestPermissions 方法后,系統(tǒng)會彈出一個(gè)權(quán)限申請的對話框,用戶可以選擇同意或拒絕權(quán)限申請,不論同意與否,都會回調(diào) onRequestPermissionsResult 方法,該方法有三個(gè)參數(shù):
  • 唯一的請求碼
  • 存儲被申請權(quán)限名的 String 數(shù)組
  • 授權(quán)結(jié)果 grantResults
// 權(quán)限申請對話框點(diǎn)擊結(jié)果回調(diào)@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {switch (requestCode) {case 1:if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {call();} else {Toast.makeText(this, "用戶拒絕授權(quán)", Toast.LENGTH_LONG).show();}break;default:}if(!ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE)){AlertDialog.Builder dialog = new AlertDialog.Builder(this);dialog.setTitle("電話權(quán)限不可用").setMessage("請?jiān)?應(yīng)用設(shè)置-權(quán)限中,允許APP使用電話權(quán)限。");dialog.setCancelable(false);dialog.setPositiveButton("立即設(shè)置", (dialog1, which) -> goToAppSetting());dialog.setNegativeButton("取消", (dialog2, which) -> dialog2.dismiss());dialog.show();}}

shouldShowRequestPermissionRationale 方法的返回值:

  • 應(yīng)用第一次安裝,并且權(quán)限被禁用時(shí),返回 true
  • 權(quán)限第一次被禁用時(shí),返回 true
  • 權(quán)限被禁用且不再提示時(shí),返回 false
  • 已授權(quán)時(shí)返回 false

總結(jié):該方法返回值表示需不需要向用戶解釋一下你的 app 為什么需要這個(gè)權(quán)限。當(dāng)用戶已經(jīng)授權(quán)或者用戶明確禁止(權(quán)限被禁用且不再提示)的時(shí)候就不需要再去解釋了,所以此時(shí)會返回 false。

權(quán)限不可用時(shí)引導(dǎo)用戶手動啟用權(quán)限:

// 跳轉(zhuǎn)到權(quán)限設(shè)置界面private void goToAppSetting() {Intent intent = new Intent();intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);Uri uri = Uri.fromParts("package", getPackageName(), null);intent.setData(uri);startActivity(intent);}

上述代碼的運(yùn)行邏輯是:

  • 通過 checkSelfPermission 檢驗(yàn)用戶是否已授權(quán):
    • 已授權(quán)則直接調(diào)用 call 打電話;
    • 未授權(quán)則通過 requestPermissions 申請授權(quán):
      • 第一次申請授權(quán)被拒絕,點(diǎn)擊按鈕仍會二次調(diào)用 requestPermissions,此時(shí) shouldShowRequestPermissionRationale 返回值為 true;
      • 第二次申請授權(quán)被拒絕,權(quán)限被視為禁止使用,調(diào)用 requestPermissions 不會再彈出詢問彈窗,但是仍會回調(diào) onRequestPermissionsResult,此時(shí) shouldShowRequestPermissionRationale 返回值為 false,因此會彈出對話框詢問用戶是否要跳轉(zhuǎn)到設(shè)置界面開啟權(quán)限,用戶可以通過 “立即設(shè)置” 跳轉(zhuǎn)到 setting界面 來開放權(quán)限,此后再點(diǎn)擊按鈕會因?yàn)橐咽跈?quán)而不再調(diào)用 requestPermissions 。

點(diǎn)擊按鈕的運(yùn)行結(jié)果:

點(diǎn)擊 DENY:


內(nèi)容提供器

內(nèi)容提供器有兩種:已有的(如 Android 系統(tǒng)自帶的電話簿、短信等程序提供的供其他程序訪問部分內(nèi)部數(shù)據(jù)外部訪問接口)、自實(shí)現(xiàn)的

ContentResolver類 是內(nèi)容提供器的具體類,可以通過 Context類 中的 getContentResolver()方法 獲取該類的實(shí)例,該類提供了一系列的 CRUD 操作,這些增刪改查方法都使用 Uri參數(shù) 替代 表名參數(shù)內(nèi)容URI 主要由三部分組成:

  • content: 協(xié)議聲明;
  • authority: 用于區(qū)分不同應(yīng)用程序,一般采用程序包名命名;
  • path: 用區(qū)分同一程序中不同表。

舉個(gè)例子:

內(nèi)容URI 只是一串字符,還需通過 Uri.parse() 方法解析成 Uri對象 才可做為參數(shù)。

關(guān)于內(nèi)容提供器的增刪查改方法,這里僅解釋較為復(fù)雜的 query() 方法:

查詢完后返回一個(gè) Cursor對象,可以通過遍歷其所有行來得到每一行數(shù)據(jù)。


運(yùn)用安卓封裝好的內(nèi)容提供器

運(yùn)用聯(lián)系人應(yīng)用的內(nèi)容提供器,讀取聯(lián)系人信息并在 ListView 中顯示。

聲明權(quán)限:

布局文件 contacts_layout.xml:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/contacts_view"android:layout_width="match_parent"android:layout_height="match_parent"/> </LinearLayout>

活動文件 ContactsActivity:

public class ContactsActivity extends AppCompatActivity {ArrayAdapter<String> adapter;List<String> contactsList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.contacts_layout);ListView contactsView = findViewById(R.id.contacts_view);adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, contactsList);contactsView.setAdapter(adapter);if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CONTACTS}, 1);}else {readContacts();}}private void readContacts() {Cursor cursor = null;try {Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;cursor = getContentResolver().query(uri, null, null,null, null, null);if(cursor != null){while(cursor.moveToNext()){// 獲取聯(lián)系人姓名String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// 獲取聯(lián)系人手機(jī)號String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));contactsList.add(name + "\n" + number);}// 刷新ListViewadapter.notifyDataSetChanged();// 關(guān)閉 Cursor 對象cursor.close();}} catch (Exception e) {e.printStackTrace();} finally {// 和上面的關(guān)閉二選一/*if(cursor != null){cursor.close();}*/}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {switch (requestCode){case 1:if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){readContacts();}else {Toast.makeText(this, "用戶拒絕授權(quán)", Toast.LENGTH_LONG).show();}break;}} }

運(yùn)行結(jié)果:


自實(shí)現(xiàn)的內(nèi)容提供器

概念

可以通過新建一個(gè) ContentProvider子類 的方式來創(chuàng)建自己的內(nèi)容提供器。ContentProvider類 有 6 個(gè)抽象方法需要我們重寫:onCreate()、query()、insert()、update()、delete()、getType()。這里重點(diǎn)介紹 onCreate 和 getType 兩個(gè)方法:

  • onCreate(): 當(dāng) ContentProvider 嘗試訪問程序中數(shù)據(jù)時(shí),初始化內(nèi)容提供器,通常在這里完成對數(shù)據(jù)庫的創(chuàng)建和升級等操作。返回 true 表內(nèi)容提供器初始化成功,false 表失敗。
  • getType(): 根據(jù)傳入的 內(nèi)容URI 來返回相應(yīng)的 MIME 類型。MIME字符串 主要由三部分組成:
  • 必須要以 vnd 開頭
  • 如果 內(nèi)容URI路徑 結(jié)尾,則后接 android.cursor.dir/,如果以 id 結(jié)尾,則后接 android.cursor.item/
  • 最后接上 vnd.<authority>.<path>

內(nèi)容URI 的格式主要有兩種:

  • 路徑結(jié)尾表示期望訪問表中所有數(shù)據(jù): content://com.example.app.provider/table (訪問 table 表中所有數(shù)據(jù))
  • id 結(jié)尾表示期望訪問表中擁有相應(yīng) id 的數(shù)據(jù):content://com.example.app.provider/table/1 (訪問 table 表中 id 為 1 的數(shù)據(jù))

還可以使用通配符:

  • 匹配任意表:content://com.example.app.provider/*
  • 匹配 table 表中任意一行數(shù)據(jù):content://com.example.app.provider/table/#

內(nèi)容URI 對應(yīng)的 MIME類型

  • content://com.example.app.provider/table : vnd.android.cursor.dir/vnd.com.example.app.provider.table
  • content://com.example.app.provider/table/1 :vnd.android.cursor.item/vnd.com.example.app.provider.table

如何匹配 內(nèi)容URI 呢?

首先借助 UriMatcher.addURI() 方法,將 內(nèi)容URI的相關(guān)信息 添加進(jìn)匹配器中,相關(guān)信息對應(yīng)方法的三個(gè)參數(shù):authority、path、(int)code。前兩者之前講過這里不再贅述,code 用以唯一標(biāo)識要訪問的資源

再借助 UriMatcher.match() 方法,傳入一個(gè) Uri對象 ,通過返回的 code 來匹配對應(yīng)的操作。

如何保證隱私數(shù)據(jù)不泄露?

因?yàn)樗械?CRUD操作 都需要匹配到相應(yīng)的 內(nèi)容URI 格式才能進(jìn)行,只要不向 UriMatcher 中添加 隱私數(shù)據(jù)的URI 就好。


實(shí)現(xiàn)

那現(xiàn)在開始自實(shí)現(xiàn)內(nèi)容提供器,操作的數(shù)據(jù)庫是該篇博客中的例子:

在 AndroidManifest.xml 文件中注冊:

自定義的內(nèi)容提供器 MyContentProvider:

public class MyContentProvider extends ContentProvider {public static final int STUDENT_DIR = 0;public static final int STUDENT_ITEM = 1;public static final int CLASS_DIR = 2;public static final int CLASS_ITEM = 3;public static final String AUTHORITY = "com.example.activitytest.CustomType.provider";private static UriMatcher uriMatcher;private MyDatabaseHelper dbHelper;static {uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);uriMatcher.addURI(AUTHORITY, "student", STUDENT_DIR);uriMatcher.addURI(AUTHORITY, "student/#", STUDENT_ITEM);uriMatcher.addURI(AUTHORITY, "class", CLASS_DIR);uriMatcher.addURI(AUTHORITY, "class/#", CLASS_ITEM);}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int deleteRows = 0;switch (uriMatcher.match(uri)){case STUDENT_DIR:deleteRows = db.delete("Student", selection, selectionArgs);break;case STUDENT_ITEM:String studentId = uri.getPathSegments().get(1);deleteRows = db.delete("Student", "id = ?", new String[]{studentId});break;case CLASS_DIR:deleteRows = db.delete("Class", selection, selectionArgs);break;case CLASS_ITEM:String classId = uri.getPathSegments().get(1);deleteRows = db.delete("Class","id = ?", new String[]{classId});break;}return deleteRows;}@Overridepublic String getType(Uri uri) {switch (uriMatcher.match(uri)){case STUDENT_DIR:return "vnd.android.cursor.dir/vnd.com.example.activitytest.CustomType.provider.student";case STUDENT_ITEM:return "vnd.android.cursor.item/vnd.com.example.activitytest.CustomType.provider.student";case CLASS_DIR:return "vnd.android.cursor.dir/vnd.com.example.activitytest.CustomType.provider.class";case CLASS_ITEM:return "vnd.android.cursor.item/vnd.com.example.activitytest.CustomType.provider.class";}return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = dbHelper.getWritableDatabase();Uri uriReturn = null;switch (uriMatcher.match(uri)){case STUDENT_DIR:case STUDENT_ITEM:long studentId = db.insert("Student", null, values);uriReturn = Uri.parse("content://" + AUTHORITY + "/student/" + studentId);break;case CLASS_DIR:case CLASS_ITEM:long classId = db.insert("Class", null, values);uriReturn = Uri.parse("content://" + AUTHORITY + "/class/" + classId);break;default:break;}return uriReturn;}@Overridepublic boolean onCreate() {dbHelper = new MyDatabaseHelper(getContext(), "Student.db", null, 4);return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder) {SQLiteDatabase db = dbHelper.getReadableDatabase();Cursor cursor = null;switch (uriMatcher.match(uri)){case STUDENT_DIR:cursor = db.query("Student", projection, selection, selectionArgs,null, null, sortOrder);break;case STUDENT_ITEM:// Uri字符串中以 “/” 作為分割,0部分是路徑,1部分則是id。即獲取Uri字符串中的id部分。String studentId = uri.getPathSegments().get(1);cursor = db.query("Student", projection, "id = ?", new String[]{ studentId }, null, null, sortOrder);break;case CLASS_DIR:cursor = db.query("Class", projection, selection, selectionArgs,null, null, sortOrder);break;case CLASS_ITEM:String classId = uri.getPathSegments().get(1);cursor = db.query("Class", projection, "id = ?", new String[]{ classId }, null, null, sortOrder);break;default:break;}return cursor;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int updateRows = 0;switch (uriMatcher.match(uri)){case STUDENT_DIR:updateRows = db.update("Student", values, selection, selectionArgs);break;case STUDENT_ITEM:String studentId = uri.getPathSegments().get(1);updateRows = db.update("Student", values, "id = ?", new String[]{studentId});break;case CLASS_DIR:updateRows = db.update("Class", values, selection, selectionArgs);break;case CLASS_ITEM:String classId = uri.getPathSegments().get(1);updateRows = db.update("Class", values, "id = ?", new String[]{classId});break;default:break;}return updateRows;} }

onCreate()

  • 初始化一個(gè) MyDatabaseHelper 實(shí)例;
  • 返回 true 表示內(nèi)容提供器初始化成功。
  • query()

  • 通過 MyDatabaseHelper 獲取 SQLiteDatabase 實(shí)例;
  • 通過 uriMatcher.match(uri) 分析用戶想訪問的表;
  • 通過 SQLiteDatabase.query() 進(jìn)行查詢,并返回 Cursor 對象:
    • 訪問單條數(shù)據(jù)時(shí),調(diào)用 uri.getPathSegments() 將 內(nèi)容URI 權(quán)限之后的部分以 “/” 作為分割,并將結(jié)果放入一個(gè)字符串列表,列表的第0個(gè)位置是路徑,第1個(gè)位置則是id
  • insert()

    • 前兩步同 query()
  • 通過 SQLiteDatabase.insert() 進(jìn)行添加,但由于該方法要求返回一個(gè) Uri對象,因此需要調(diào)用 Uri.parse() 將 URI字符串 解析成 Uri對象
  • 接下來新建一個(gè)程序,用來調(diào)用上面的內(nèi)容提供器:

    public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private String newId;public static final String AUTHORITY = "content://com.example.activitytest.CustomType.provider/";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button_add = findViewById(R.id.button_add);button_add.setOnClickListener(v->{Uri uri = Uri.parse(AUTHORITY + "student/");ContentValues values = new ContentValues();values.put("name", "zj");values.put("age", 21);values.put("weight", 90);values.put("gender", "girl");Uri insertUri = getContentResolver().insert(uri, values);newId = insertUri.getPathSegments().get(1);Log.e(TAG, "咕咕:"+insertUri.toString());});Button button_query = findViewById(R.id.button_query);button_query.setOnClickListener(v->{Uri uri = Uri.parse(AUTHORITY + "student");Cursor cursor = getContentResolver().query(uri, null, null,null, null);while(cursor.moveToNext()){String name = cursor.getString(cursor.getColumnIndex("name"));int age = cursor.getInt(cursor.getColumnIndex("age"));double weight = cursor.getDouble(cursor.getColumnIndex("weight"));String gender = cursor.getString(cursor.getColumnIndex("gender"));String res = name + " " + age + " " + weight + " " + gender;Toast.makeText(this, res, Toast.LENGTH_LONG).show();}cursor.close();Log.e(TAG, "表中數(shù)據(jù)顯示完畢");});Button button_update = findViewById(R.id.button_update);button_update.setOnClickListener(v->{Uri uri = Uri.parse(AUTHORITY + "student/" + newId);ContentValues values = new ContentValues();values.put("name", "cjl");values.put("weight", 95);getContentResolver().update(uri, values, null, null);});Button button_delete = findViewById(R.id.button_delete);button_delete.setOnClickListener(v->{if(newId!=null && newId.compareTo("0") > 0){Uri uri = Uri.parse(AUTHORITY + "student/" + newId);getContentResolver().delete(uri, null, null);newId = String.valueOf(Integer.valueOf(newId)-1);Log.e(TAG, "最后一個(gè)id:" + newId);}else{Toast.makeText(this, "表中已經(jīng)沒有數(shù)據(jù)了", Toast.LENGTH_LONG).show();}});} }

    如果模擬器是 Android 11,那么該程序的清單文件需要加上 <queries> 標(biāo)簽,原因見本博客:

    總結(jié)

    以上是生活随笔為你收集整理的Android入门(13)| Android权限 与 内容提供器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。