搜索

移动开发技术作业 作业三


发布时间: 2022-11-24 19:49:00    浏览次数:45 次

设计目标:

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;

2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。 

3、本次作业请启用新项目,理论上需要两个APP进行实验。

 功能说明:
package com.example.resolve;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentResolver resolver=getContentResolver();

        ContentValues values=new ContentValues();
        values.put("name","cc");
        values.put("age",20);
        Uri uri=Uri.parse("content://wcc.provider1/person");
        Integer.parseInt("20");
        Button button1=findViewById(R.id.button);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resolver.insert(uri,values);
            }
        });
    }
}

这个代码的目的是通过数据库的资源地址将数据插入数据库。为了做到这一点,还需要关联权限。加上了权限,这个接收APP也算是做好了。

<queries>
        <package android:name="com.example.contentprovider"/>
    </queries>


package
com.example.contentprovider; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class MyDBhelper extends SQLiteOpenHelper { public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table student(id integer primary key autoincrement " + ",name varchar(20) ," + "age integer)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }

这段代码的目的是创建一个数据库

public class MyContentProvider extends ContentProvider {
    public MyDAO myDAO;
    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.
        Context context=getContext();
        myDAO=new MyDAO(context);
        return true;
    }

    @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");
    }
}

通过contentprovisder实现数据库操作的创建。

public class MyDAO {
    private SQLiteDatabase database;
    private SQLiteOpenHelper myopenhelper;
    private Context context;

    private Uri uri=Uri.parse("content://wcc.provider1");  //.../person/10  指的是person表第10行

    public MyDAO(Context context){
        this.context=context;
        myopenhelper=new MyDBhelper(context,"wccDB",null,1);
        database=myopenhelper.getReadableDatabase();

        database.execSQL("drop table if exists person");
        database.execSQL("create table person(id integer primary key autoincrement,"+" name varchar, age integer)");
    }

    public Uri addvalue(Uri uri, ContentValues values){
        long rowID=database.insert("person",null,values);

        if(rowID == -1){
            Log.d("DAO","数据插入失败");
            return  null;
        }
        else {
            Uri insertUri= ContentUris.withAppendedId(uri,rowID);
            Log.d("wcc","ContentUris:"+insertUri.toString());
            context.getContentResolver().notifyChange(insertUri,null);
            return insertUri;
        }
    }
}

这段代码主要实现了对数据库的操作。

 运行展示截图:

 

 仓库地址:https://github.com/mostimacc/contentprovider.git

https://github.com/mostimacc/resolve.git

 

 

 

 

 
免责声明 移动开发技术作业 作业三,资源类别:文本, 浏览次数:45 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 07:49:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/mostimacc/p/16922993.html