【Android】_MyContentProvider_外部访问接口

一、新建MycontentProvider
在这里插入图片描述
权限设置:
在这里插入图片描述
MyContentProvider.java:

package com.example.z.student;

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

public class MyContentProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        return false;
    }


    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {

        MySQLiteAccess database=new MySQLiteAccess(this.getContext(),3);
        SQLiteDatabase sqLiteDatabase=database.getReadableDatabase();

        String sql="select *from students ";
        Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
        System.out.println("查询成功!");
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}

二、接收端:(另一个app)
在这里插入图片描述

java:

package com.example.cungu.myapplication4;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URI;

public class StuActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView texts;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu);
        texts=(TextView)findViewById(R.id.texts);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                search();
                break;
        }
    }
    public void insert(){
        ContentResolver resolver = getContentResolver();// 获取ContentResolver
        Uri uri = Uri.parse("content://provider/student");
        // 插入表中数据
        ContentValues values = new ContentValues();
        values.put("id", "6");
        values.put("name", "test");
        values.put("sex", "男");
        values.put("age", "20");
        values.put("academy", "信息科学与工程");
        values.put("major", "物联网工程");
        values.put("date", "19980101");
        // 通过ContentResolver 根据URI 向ContentProvider中插入数据
        Uri result=resolver.insert(uri, values);
        String n=result.getPath();
        Toast.makeText(this, n, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "插入成功!", Toast.LENGTH_SHORT).show();
    }
    public void search(){
        ContentResolver resolver=getContentResolver();
        Uri uri=Uri.parse("content://Student");
        Cursor cursor=resolver.query(uri,null,null,null,null);
        if(cursor!=null)
            while (cursor.moveToNext()){
                String id=cursor.getString(cursor.getColumnIndex("id"));
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String sex=cursor.getString(cursor.getColumnIndex("sex"));
                String age=cursor.getString(cursor.getColumnIndex("age"));
                String academy=cursor.getString(cursor.getColumnIndex("academy"));
                String major=cursor.getString(cursor.getColumnIndex("major"));
                String date=cursor.getString(cursor.getColumnIndex("date"));
                texts.append("\n"+id+" "+name+" "+sex+"  "+age+"  "+academy+"  "+major+"  "+date+"  ");
            }
    }
}

三、应用效果:

student.app:
在这里插入图片描述
在这里插入图片描述


这里为select * from students,可以加细一点,弄成关键词搜索。

猜你喜欢

转载自blog.csdn.net/cungudafa/article/details/85779137