사용 컨텐트 프로 컨텐츠 제공

소개 컨텐트 프로 컨텐츠 제공

1.ContentProvider 콘텐츠 제공자 (네 개의 구성 요소 중 하나)는 주로 다른 애플리케이션 사이에서 데이터를 공유하는 기능이다.
: 2.ContentProvider이 세 가지 우선 순위는
(1) 컨텐트 프로 콘텐츠 제공자
(2) contentResolve 콘텐츠 리졸버
(3) URI 주소
여기가 간단하고, "서버"와 유사한 컨텐트 프로 우리가 자주 언급하는 방법을 이해하고, contentResolve는 우리의 "고객이다 액세스와 같은 URI에 "끝.

컨텐츠 공급자를 만들

우측으로 -> 콘텐츠 제공자는 콘텐츠 공급자를 만들려면> 새로 만들기 -> 기타 -

그림 삽입 설명 여기
이 밖으로 만들기 다음과 같이하는 것입니다 :

public class MyContentProvider extends ContentProvider {
    public MyContentProvider() {
    }
    
    //删除
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //返回类型
    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    //插入
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //创建数据库
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        return false;
    }

    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //更新
    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

우리는 당신이 방법에있어서 로직 코드를 작성하는 일을 할 필요가
(SQLite는 데이터베이스가 밝은 전망 기사를)

/**
 * 内容提供者
 */
public class MyContentProvider extends ContentProvider {
    private MySqlHelper mySqlHelper;
    private SQLiteDatabase readableDatabase;


    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int student = readableDatabase.delete("student", selection, selectionArgs);
        return student;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //需要补null值
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        readableDatabase.insert("student",null,values);
        return uri;
    }

    @Override
    public boolean onCreate() {
        mySqlHelper = new MySqlHelper(getContext(),"user.db",null,1);
        readableDatabase = mySqlHelper.getReadableDatabase();
        if (readableDatabase!=null){
            return true;
        }
        return false;
    }

    //查询中有6个参数,但是其中有两个系统没有提供,不需要时要补上null值
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor student = readableDatabase.query("student", projection, selection,selectionArgs, null, null, sortOrder);
        return student;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int student = readableDatabase.update("student", values, selection,selectionArgs);
        return student;
    }
}

컨텐츠 해결 만들기

우리가 원하는 것은 응용 프로그램 데이터베이스에서 작동하기 때문에 여기에서 우리는하지만, 약간의 의미,이 응용 프로그램에서 만들 수있는

//创建内容解析者
private ContentResolver contentResolver = getContentResolver();

우리는 URI를 가지고, URI는 우리의 공급자 계약이 작성 내용
* 참고 내용 : //하지 지방

Uri uri = Uri.parse("content://com.highday9");

전체 코드 :

public class MainActivity extends AppCompatActivity {
    private Button btnUpdata;
    private Button btnDelete;
    //内容解析者
    private ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnUpdata = (Button) findViewById(R.id.btn_updata);
        btnDelete = (Button) findViewById(R.id.btn_delete);

        final Uri uri = Uri.parse("content://com.highday9");
        contentResolver = getContentResolver();

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentResolver.delete(uri,"name=?",new String[]{"xu"});
            }
        });

    }
}

사용자 권한

소개

정의 권한은 일반적으로 보안을 향상시키기 위해 노출 된 구성 요소에 사용됩니다. 안드로이드는 다른 응용 프로그램 (서버)되는 구성 요소를 호출하는 응용 프로그램 (클라이언트)를 할 수 있습니다. 응용 프로그램 서버가 해당 구성 요소를 노출 한 것처럼 그래서, 클라이언트 응용 프로그램에 액세스 할 수 있습니다. 구성 요소가 구성 요소 호출 올 수 후, 허가없이 다른 응용 프로그램 중 하나를 노출되면 물론, 노출시, 권한은 필요하지, 구성 요소가 권한을 적용 할 경우, 구성 요소를 호출 할 수있는 권한을 소유 응용 프로그램 만 .

권한을 사용자 지정하는 방법

여기에 읽기 및 쓰기 권한은 예를 들자면 :
사용자 지정 권한은 외부 응용 프로그램에서 쓰기에 필요한

<!--自定义权限-->
<permission
    android:name="com.zhao.permission.READ"
    android:protectionLevel="normal"></permission>

<permission
    android:name="com.zhao.permission.WRITE"
    android:protectionLevel="normal"></permission>

사용자 권한 속성 :

name,该标签就是权限的名字。
description,该标签就是权限的介绍。
permissionGroup,指定该权限的组。
protectionLevel,指定保护级别。一般使用normal
Android将权限分为若干个保护级别,normal, dangerous, signature等。normal就是正常权限,该权限并不会给用户或者设备的隐私带来风险;dangerous就是危险权限,该级别的权限通常会给用户的数据或设备的隐私带来风险;signature指的是,只有相同签名的应用才能使用该权限。

추가 권한

<provider
    android:readPermission="com.zhao.permission.READ"
    android:writePermission="com.zhao.permission.WRITE"
    android:name=".MyContentProvider"
    android:authorities="com.highday9"
    android:enabled="true"
    android:exported="true"></provider>

전체 코드 :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.highday9">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
        <provider
            android:readPermission="com.zhao.permission.READ"
            android:writePermission="com.zhao.permission.WRITE"
            android:name=".MyContentProvider"
            android:authorities="com.highday9"
            android:enabled="true"
            android:exported="true"></provider>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!--自定义权限-->
    <permission
        android:name="com.zhao.permission.READ"
        android:protectionLevel="normal"></permission>

    <permission
        android:name="com.zhao.permission.WRITE"
        android:protectionLevel="normal"></permission>

</manifest>

설정 사용자 지정 권한

같은 이름의 값으로 응용 프로그램의 매니페스트 파일, 급여의 관심에 추가

<!-- 添加自定义读写权限 -->
<uses-permission android:name="com.zhao.permission.READ" />
<uses-permission android:name="com.zhao.permission.WRITE" />
게시 11 개 원래 기사 · 원의 칭찬 0 · 조회수 2404

추천

출처blog.csdn.net/weixin_45697390/article/details/104615868