ContentProvide-安卓四大组件(内容提供者)

1.ContentProvide是什么

为了跨进程通信,访问其他的app的数据库,用广播比较麻烦,所以可以使用ContentProvide将数据暴露,给其他app去调用自己的数据,例如微信获取通讯录的数据。

2.ContentResoler是什么

ContentProvide将数据暴露,就需要有类去接受和调用里面的方法,ContentResoler就是用来接收ContentProvide数据的类

3.Uri是什么,标准格式是什么

"content://com.example.ContentTest/student"

这是一个Uri的标准格式,”content://”和url中的”https://”相同”com.example.ContentTest”是主机名,只能有俩个”.”号,一般为包名,”/student”是path(路径)。

4.UriMatcher是什么

UriMatcher本质是一个文本过滤器,可以直接得到Uri的path,可以通过调用uriMatcher.addURI()方法添加相对应的返回码

uriMatcher.addURI("content://com.example.ContentTest", "student", 1);

5.如何使用ContentProvider

先定义一个类继承ContentProvider,实现它的几个方法

package com.example.myapplication;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

public class Centet extends ContentProvider {
    String TAG="Centet";
    private MySQLife mySQLife;
    private SQLiteDatabase sqLiteDatabase;
    private  UriMatcher uriMatcher;
    {
       uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
       uriMatcher.addURI("content://com.example.mycentet/student","student",1);
       uriMatcher.addURI("content://com.example.mycentet/student","teacher",2);
    }
    @Override
    public boolean onCreate() {
        Log.e(TAG, "onCreate: " );
        mySQLife=new MySQLife(getContext(),"student_rt",null,1);
        sqLiteDatabase=mySQLife.getWritableDatabase();
        return false;
    }


    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.e(TAG, "query: ...................");
        Cursor cursor=sqLiteDatabase.query("student",null,null,null,null,null,null);
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.e(TAG, "getType: ......................." );
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.e(TAG, "insert: .....................");
        sqLiteDatabase.insert("student",null,values);
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.e(TAG, "delete: ...................");
        sqLiteDatabase.delete("student",selection,selectionArgs);
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.e(TAG, "update: .........................." );
        sqLiteDatabase.update("student",values,selection,selectionArgs);
        return 0;
    }
}

在mainfest里面注册

 "com.example.mycentet"
            android:name=".Centet"
            android:exported="true"//是否暴露数据,默认为false
            />

在其他app接收

package com.example.myapplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private EditText nameET;
    private EditText ageET;
    private Button chaxunBtn;
    private Button addBtn;
    private Button updataBtn;
    private Button deleteBtn;
    private ListView sqlifeLv;
    private List studentList=new ArrayList<>();
    private MyAdapter myAdapter;
    private MySQLife mySQLife=new MySQLife(this,"student_rt",null,1);
    private SQLiteDatabase sqLiteDatabase;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            sqlifeLv.setAdapter(myAdapter);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindID();
        sqLiteDatabase=mySQLife.getWritableDatabase();
        myAdapter=new MyAdapter(this,studentList);
    }

    private void bindID() {
        nameET = findViewById(R.id.name_et);
        ageET = findViewById(R.id.age_et);
        chaxunBtn = findViewById(R.id.chaxun_btn);
        addBtn = findViewById(R.id.add_btn);
        updataBtn = findViewById(R.id.update_btn);
        deleteBtn = findViewById(R.id.delete_btn);
        chaxunBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        updataBtn.setOnClickListener(this);
        sqlifeLv = findViewById(R.id.sqlife_lv);

    }

    @Override
    public void onClick(View v) {
        ContentValues values=new ContentValues();
        String name=nameET.getText().toString();
        String age=ageET.getText().toString();
        switch (v.getId()) {
            case R.id.add_btn:
                values.put("name",name);
                values.put("age",age);
                sqLiteDatabase.insert("student",null,values);
                break;
            case R.id.delete_btn:
                sqLiteDatabase.delete("student","name=?",new String[]{name});
                break;
            case R.id.update_btn:
                values.put("name","ssss");
                sqLiteDatabase.update("student",values,"name=?",new String[]{name});
                break;
            case R.id.chaxun_btn:
                studentList.clear();
                 new Thread(new Runnable() {
                     @Override
                     public void run() {
                         Cursor cursor=sqLiteDatabase.query("student",null,null,null,null,null,null);
                         cursor.moveToFirst();
                         do {
                             String name1=cursor.getString(cursor.getColumnIndex("name"));
                             int i=cursor.getInt(cursor.getColumnIndex("id"));
                             String age2=cursor.getString(cursor.getColumnIndex("age"));
                             Student student=new Student(i,age2,name1);
                             studentList.add(student);
                         }while (cursor.moveToNext());
                         handler.sendEmptyMessage(1);
                     }
                 }).start();
                  break;
            default:
                break;
        }
    }
}

你可能感兴趣的:(初级安卓开发)