android文件权限简述

image.png
image.png

内容提供者(ContentProvider)

image.png
image.png
image.png

内容解析者(ContentResolver)

image.png
image.png
image.png
image.png
image.png

综合实例-联系人信息管理

关键代码

image.png
image.png
image.png

MyContentProvider.java

  1. package com.example.contentproviderdemo;
  2. import android.content.ContentProvider;
  3. import android.content.ContentValues;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.util.Log;
  7. public class MyContentProvider extends ContentProvider {
  8. public MyContentProvider() {
  9. }
  10. @Override
  11. public int delete(Uri uri, String selection, String[] selectionArgs) {
  12. // Implement this to handle requests to delete one or more rows.
  13. throw new UnsupportedOperationException("Not yet implemented");
  14. }
  15. @Override
  16. public String getType(Uri uri) {
  17. // TODO: Implement this to handle requests for the MIME type of the data
  18. // at the given URI.
  19. throw new UnsupportedOperationException("Not yet implemented");
  20. }
  21. @Override
  22. public Uri insert(Uri uri, ContentValues values) {
  23. // TODO: Implement this to handle requests to insert a new row.
  24. throw new UnsupportedOperationException("Not yet implemented");
  25. }
  26. @Override
  27. public boolean onCreate() {
  28. // TODO: Implement this to initialize your content provider on startup.
  29. return false;
  30. }
  31. @Override
  32. public Cursor query(Uri uri, String[] projection, String selection,
  33. String[] selectionArgs, String sortOrder) {
  34. // TODO: Implement this to handle query requests from clients.
  35. Log.d("15pb","MyContentProvider::query");
  36. return null;
  37. }
  38. @Override
  39. public int update(Uri uri, ContentValues values, String selection,
  40. String[] selectionArgs) {
  41. // TODO: Implement this to handle requests to update one or more rows.
  42. throw new UnsupportedOperationException("Not yet implemented");
  43. }
  44. }

MainActivity.java

  1. package com.example.contentproviderdemo;
  2. import android.content.ContentResolver;
  3. import android.database.Cursor;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. }
  15. public void btnClick(View view) {
  16. // 1. 获取内容解析者
  17. ContentResolver resolver = getContentResolver();
  18. // 2. 创建URI
  19. Uri uri = Uri.parse("content://"+"MyContentProvider");
  20. // 3. 查询
  21. Cursor cursor = resolver.query(uri,null,null,null,null,null);
  22. Log.d("15pb","cursor:"+cursor);
  23. }
  24. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn1"
  11. android:onClick="btnClick"
  12. android:text="访问内容提供者"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"></Button>
  15. </LinearLayout>