Android入门(13)| Android权限 与 内容提供器
文章目錄
- 普通權限與危險權限
- 運行時申請權限
- 內容提供器
- 運用安卓封裝好的內容提供器
- 自實現的內容提供器
- 概念
- 實現
普通權限與危險權限
主要用于不同應用程序之間在保證被訪數據的安全性的基礎上,實現數據共享的功能。
在 Android 6.0 開始引入了運行時權限的功能,用戶在安裝軟件時不需要一次性授權所有的權限,而是在軟件的使用過程中再對某一項權限進行申請。Android 將權限分為兩類:
- 普通權限: 不會直接影響到用戶的安全和隱私的權限,對于這部分權限,系統自動授權。
- 危險權限: 可能會涉及到用戶的隱私或者對設備安全性造成影響的權限。
危險權限如下,這些權限需要進行運行時權限處理,不在表中的權限只需要在 AndroidManifest.xml 添加權限聲明即可:
表中的每一個危險權限都屬于一個權限組,雖然在進行權限處理的時候使用的是權限名,但是一旦用戶同意授權,那么該權限名對應的權限組中的所有權限也會同時被授權。
運行時申請權限
給按鈕注冊點擊事件:
Button button1 = findViewById(R.id.button_1);button1.setOnClickListener((View view)->{try {/*// 打開撥號界面,無需聲明權限Intent intent = new Intent(Intent.ACTION_DIAL);*/// 打電話,需要生命權限Intent intent = new Intent(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:15309276440"));startActivity(intent);} catch (SecurityException e){e.printStackTrace();}});在注冊表中加入:
這樣的程序在 Android 6.0 之前都可以正常運行,但是在更高的版本點擊按鈕后沒有任何效果,錯誤信息如下:
權限被禁止。
修復這個問題,申請運行時權限的流程:
將打電話的行為封裝成函數 call():
private void call(){try {/*// 打開撥號界面,無需聲明權限Intent intent = new Intent(Intent.ACTION_DIAL);*/// 打電話,需要聲明權限Intent intent = new Intent(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:15309276440"));startActivity(intent);} catch (SecurityException e){e.printStackTrace();}}修改 onCreate 方法內的點擊按鈕行為:
Button button1 = findViewById(R.id.button_1);button1.setOnClickListener((View view)->{// 相等說明用戶已授權,不等說明未授權if(ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){// 申請授權ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.CALL_PHONE}, 1);} else {call();}});- 通過 ContextCompat.checkSelfPermission() 方法檢測用戶是否已授權,該方法有兩個參數:
- context
- 具體權限名
- 未授權則需要調用 ActivityCompat.requestPermissions() 方法來向用戶申請授權,該方法接受三個參數:
- Activity 實例,也就是當前活動。
- String 數組,也就是要申請的權限名。
- 請求碼,必須唯一,這里傳入 1。
- 調用 requestPermissions 方法后,系統會彈出一個權限申請的對話框,用戶可以選擇同意或拒絕權限申請,不論同意與否,都會回調 onRequestPermissionsResult 方法,該方法有三個參數:
- 唯一的請求碼
- 存儲被申請權限名的 String 數組
- 授權結果 grantResults
shouldShowRequestPermissionRationale 方法的返回值:
- 應用第一次安裝,并且權限被禁用時,返回 true
- 權限第一次被禁用時,返回 true
- 權限被禁用且不再提示時,返回 false
- 已授權時返回 false
總結:該方法返回值表示需不需要向用戶解釋一下你的 app 為什么需要這個權限。當用戶已經授權或者用戶明確禁止(權限被禁用且不再提示)的時候就不需要再去解釋了,所以此時會返回 false。
權限不可用時引導用戶手動啟用權限:
// 跳轉到權限設置界面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);}上述代碼的運行邏輯是:
- 通過 checkSelfPermission 檢驗用戶是否已授權:
- 已授權則直接調用 call 打電話;
- 未授權則通過 requestPermissions 申請授權:
- 第一次申請授權被拒絕,點擊按鈕仍會二次調用 requestPermissions,此時 shouldShowRequestPermissionRationale 返回值為 true;
- 第二次申請授權被拒絕,權限被視為禁止使用,調用 requestPermissions 不會再彈出詢問彈窗,但是仍會回調 onRequestPermissionsResult,此時 shouldShowRequestPermissionRationale 返回值為 false,因此會彈出對話框詢問用戶是否要跳轉到設置界面開啟權限,用戶可以通過 “立即設置” 跳轉到 setting界面 來開放權限,此后再點擊按鈕會因為已授權而不再調用 requestPermissions 。
點擊按鈕的運行結果:
點擊 DENY:
內容提供器
內容提供器有兩種:已有的(如 Android 系統自帶的電話簿、短信等程序提供的供其他程序訪問部分內部數據的外部訪問接口)、自實現的。
ContentResolver類 是內容提供器的具體類,可以通過 Context類 中的 getContentResolver()方法 獲取該類的實例,該類提供了一系列的 CRUD 操作,這些增刪改查方法都使用 Uri參數 替代 表名參數。內容URI 主要由三部分組成:
- content: 協議聲明;
- authority: 用于區分不同應用程序,一般采用程序包名命名;
- path: 用區分同一程序中不同表。
舉個例子:
內容URI 只是一串字符,還需通過 Uri.parse() 方法解析成 Uri對象 才可做為參數。
關于內容提供器的增刪查改方法,這里僅解釋較為復雜的 query() 方法:
查詢完后返回一個 Cursor對象,可以通過遍歷其所有行來得到每一行數據。
運用安卓封裝好的內容提供器
運用聯系人應用的內容提供器,讀取聯系人信息并在 ListView 中顯示。
聲明權限:
布局文件 contacts_layout.xml:
活動文件 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()){// 獲取聯系人姓名String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// 獲取聯系人手機號String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));contactsList.add(name + "\n" + number);}// 刷新ListViewadapter.notifyDataSetChanged();// 關閉 Cursor 對象cursor.close();}} catch (Exception e) {e.printStackTrace();} finally {// 和上面的關閉二選一/*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, "用戶拒絕授權", Toast.LENGTH_LONG).show();}break;}} }運行結果:
自實現的內容提供器
概念
可以通過新建一個 ContentProvider子類 的方式來創建自己的內容提供器。ContentProvider類 有 6 個抽象方法需要我們重寫:onCreate()、query()、insert()、update()、delete()、getType()。這里重點介紹 onCreate 和 getType 兩個方法:
- onCreate(): 當 ContentProvider 嘗試訪問程序中數據時,初始化內容提供器,通常在這里完成對數據庫的創建和升級等操作。返回 true 表內容提供器初始化成功,false 表失敗。
- getType(): 根據傳入的 內容URI 來返回相應的 MIME 類型。MIME字符串 主要由三部分組成:
- 必須要以 vnd 開頭
- 如果 內容URI 以 路徑 結尾,則后接 android.cursor.dir/,如果以 id 結尾,則后接 android.cursor.item/
- 最后接上 vnd.<authority>.<path>
內容URI 的格式主要有兩種:
- 以路徑結尾表示期望訪問表中所有數據: content://com.example.app.provider/table (訪問 table 表中所有數據)
- 以 id 結尾表示期望訪問表中擁有相應 id 的數據:content://com.example.app.provider/table/1 (訪問 table 表中 id 為 1 的數據)
還可以使用通配符:
- 匹配任意表:content://com.example.app.provider/*
- 匹配 table 表中任意一行數據:content://com.example.app.provider/table/#
內容URI 對應的 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
如何匹配 內容URI 呢?
首先借助 UriMatcher.addURI() 方法,將 內容URI的相關信息 添加進匹配器中,相關信息對應方法的三個參數:authority、path、(int)code。前兩者之前講過這里不再贅述,code 用以唯一標識要訪問的資源。
再借助 UriMatcher.match() 方法,傳入一個 Uri對象 ,通過返回的 code 來匹配對應的操作。
如何保證隱私數據不泄露?
因為所有的 CRUD操作 都需要匹配到相應的 內容URI 格式才能進行,只要不向 UriMatcher 中添加 隱私數據的URI 就好。
實現
那現在開始自實現內容提供器,操作的數據庫是該篇博客中的例子:
在 AndroidManifest.xml 文件中注冊:
自定義的內容提供器 MyContentProvider:
onCreate()
query()
- 訪問單條數據時,調用 uri.getPathSegments() 將 內容URI 權限之后的部分以 “/” 作為分割,并將結果放入一個字符串列表,列表的第0個位置是路徑,第1個位置則是id。
insert()
- 前兩步同 query()
接下來新建一個程序,用來調用上面的內容提供器:
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, "表中數據顯示完畢");});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, "最后一個id:" + newId);}else{Toast.makeText(this, "表中已經沒有數據了", Toast.LENGTH_LONG).show();}});} }如果模擬器是 Android 11,那么該程序的清單文件需要加上 <queries> 標簽,原因見本博客:
總結
以上是生活随笔為你收集整理的Android入门(13)| Android权限 与 内容提供器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GSYVideoPlayer使用总结
- 下一篇: Android入门(九)| 滚动控件 L