作业要求:

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

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

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

首先我们新建两个新的项目,分别命名为ContentProvider和Myresolver。

我们先完成Myresolver的编写:在MainActivity.java里面写:

package com.example.myresolver;import androidx.annotation.ContentView;
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 {private Button button;private ContentResolver resolver;//private  static  final String AUTHORITY="hjy.Provider1";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=findViewById(R.id.button);ContentResolver resolver=getContentResolver();Uri uri=Uri.parse("content://hjy.provider1/student");ContentValues values=new ContentValues();values.put("name","hjy");values.put("age",19);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {resolver.insert(uri,values);}});}
}

设计的resolver界面如下:

然后我们开始编写ContentProvider:

先编写数据库有关代码,新建一个DatabaseActivity和一个MyDBhelper,代码如下:

package com.example.contentprovider;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class DatabaseActivity2 extends AppCompatActivity {private Button button,button2,button3,button4;private MyDBhelper dBhelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_database2);button=findViewById(R.id.button);button2=findViewById(R.id.button2);button3=findViewById(R.id.button3);button4=findViewById(R.id.button4);MyDBhelper dBhelper=new MyDBhelper(this,"hjyDB",null,1);SQLiteDatabase database=dBhelper.getReadableDatabase();button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ContentValues values1=new ContentValues();values1.put("name","zhangsan");values1.put("age",19);ContentValues values2=new ContentValues();values2.put("name","lisi");values2.put("age",20);ContentValues values3=new ContentValues();values3.put("name","wangwu");values3.put("age",21);ContentValues values4=new ContentValues();values4.put("name","liuliu");values4.put("age",21);database.insert("student",null,values1);database.insert("student",null,values2);database.insert("student",null,values3);database.insert("student",null,values4);ContentValues values=new ContentValues();}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ContentValues values=new ContentValues();values.put("age",19);database.update("student",values,"name=?",new String[]{"liuliu"});//修改liuliu的年龄为19}});button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// database.execSQL("delete from student");//删除表的所有内容database.delete("student","name=?",new String[]{"liuliu"});//删除名字为liuliu的一行}});button4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Cursor cursor1=database.query("student",new String[]{"name"},"name=?",new String[]{"wangwu"},null,null,null);Cursor cursor2=database.rawQuery("select * from student where name=?",new String[]{"zhangsan"});//另一种查询方法Log.d("hjy",String.valueOf(cursor2.getCount()));Log.d("hjy","cursor2.getPosition:"+cursor2.getPosition());while (cursor2.moveToNext()){@SuppressLint("Range") Integer id=cursor2.getInt(cursor2.getColumnIndex("id"));@SuppressLint("Range") String name=cursor2.getString(cursor2.getColumnIndex("name"));Log.d("hjy","result"+id+name);}cursor2.close();}});}}
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,"hjyDB",factory,version);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase){sqLiteDatabase.execSQL("create table student("+"id integer primary key autoincrement,name varchar(20),age interger)");}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase,int i,int i1){}
}

DatabaseActivity的界面如下:

然后我们编写ContentProvider.java:

package com.example.contentprovider;import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;public class MyContentProvider extends ContentProvider {private  MyDAO myDAO;private Context context;@Overridepublic 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");}@Overridepublic 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");}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO: Implement this to handle requests to insert a new row.//throw new UnsupportedOperationException("Not yet implemented");return myDAO.hjyInsert(values);}@Overridepublic boolean onCreate() {// TODO: Implement this to initialize your content provider on startup.context=this.getContext();myDAO=new MyDAO(context);return true;}@Overridepublic 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");}@Overridepublic 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");}
}

MyDao.java:

package com.example.contentprovider;import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;public class MyDAO {private Context context;private Uri uri=Uri.parse("content://hjy.provider1");public  MyDAO(Context context){this.context=context;}// MyDBhelper dBhelper=new MyDBhelper(context,"hjyDB",null,1);// SQLiteDatabase database=dBhelper.getWritableDatabase();public Uri hjyInsert(ContentValues values){MyDBhelper dBhelper=new MyDBhelper(context,"hjyDB",null,1);SQLiteDatabase database=dBhelper.getWritableDatabase();ContentValues values1=new ContentValues();values1.put("name","zhangsan");values1.put("age",19);Uri uri=Uri.parse("content://hjy.provider1/student");long rowId= database.insert("student",null,values1);Uri  inserturi= ContentUris.withAppendedId(uri,rowId);return inserturi;}
}

当然我们还要修改Mainfest里的内容,首先要增加permission

修改provider里的内容

以上就是代码编写的内容,然后我们运行contentprovider和resolver

运行结果:

点击resolver里的button前:

点击后:

仓库:胡宝宝/hubaobaoh

Android 第三次作业 contentprovider与resolver相关推荐

  1. android第三次作业

    一.实现的功能 1.实现播放,暂停,停止,播放上一首,下一首功能 2.实现了进度条与歌曲的匹配 3.实现了播放时候的专辑旋转效果 4.实现了播放列表 二.项目截图 播放界面 歌曲界面 三.主要代码 相 ...

  2. 1600802047 android 第三次作业(音乐播放器)

    一.实现的功能 播放.暂停.上一首.下一首    显示列表 二.UI界面截图 第一首歌 第二首歌 第三首歌 第四首歌 list列表 点击播放音乐时图片旋转,点击上一首切换上一首歌,专辑图片和歌曲信息跟 ...

  3. android 实训的背景,Android实训项目作业.doc

    Android实训项目作业 2-1用整型数计算两个数的和2 2-7排列任意4个数的顺序,按从小到大顺序输出2 2.1编写显示下列图形的程序.2 3.1编写程序,当点击按钮命令后,页面标题及文本组件的文 ...

  4. 2017-2018-1 JAVA实验站 第三周作业

    2017-2018-1 JAVA实验站 第三周作业 团队展示 队名 JAVA实验站 拟作的团队项目描述 (2048)增加其他模式,使得2048更加丰富多彩 团队的首次合照 团队的特色描述 团队内部很团 ...

  5. Android学习---通过内容提供者(ContentProvider)操作另外一个应用私有数据库的内容...

    一.什么是ContentProvider? ContentProvider直译过来就是内容提供者,主要作用就是A应用提供接口给B应用调用数据,和之前介绍的sharedPreference和直接开放文件 ...

  6. Android studio三周学习总结

    因为疫情我们在线上已经进行了三个周的网课学习, 这半个月一直在学习Android方面的知识,对Android开发有了一个基本的认识,学会了Android studio的基本操作.刚开始接触Androi ...

  7. Android课程设计大作业-音乐播放器

    Android课程设计大作业-音乐播放器 一.**主要实现界面效果** 1)登录界面 2)音乐列表界面 3)音乐播放界面 二.**系统设计** 1)使用Service播放音乐 2) 前台界面(Acti ...

  8. OO第三单元作业总结

    OO第三次作业总结 一.JML (一)JML语言理论基础 (1)JML表达式: JML表达式包括以下几种: 原子表达式如\result(方法执行后的返回值).\old(表达式在相应方法执行前的取值): ...

  9. 程序设计第三次作业附加 代码规范

    题目:第三次作业附加 myGithub 我的程序设计第三次作业 Calculator.h //==============================// //文件名称:calculator.h ...

最新文章

  1. notification antd 弹窗使用示例
  2. CentOS 6.3下Strongswan搭建IPSec ***(ipsec.conf配置文件有讲解)
  3. 细说PHP中strlen和mb_strlen的区别
  4. 机器学习笔记 时间序列预测(基本数据处理,Box-Cox)
  5. 批量生成 Gitee 仓库克隆命令的方法
  6. 2022年中国足球球迷行为洞察白皮书
  7. seleniumphantomJs相关
  8. 重启服务器导致网站系统错误,win10怎么总是莫名其妙重启?_网站服务器运行维护...
  9. Intel Sandy Bridge/Ivy Bridge架构/微架构/流水线 (16) - L1数据缓存/存储转发访存消歧存储体冲突
  10. 内核进程回调遍历【记录】
  11. 【互亿无线】如何选择国际短信服务商
  12. python 折线图标签_如何使用python绘制折线图?
  13. 如何检测猥琐的私有SDWAN隧道协议
  14. Python3实现发送邮件、发送图片、附件等
  15. 〖经典怀念〗新白娘子传奇MV之青姐17部完整原版下载
  16. 科技助力东京奥运会:中国装备中国造
  17. torch.roll() 用法解读
  18. javaMail发送邮件读取流中的数据并作为作为附件发送邮件
  19. 2017清北学堂(提高组精英班)集训笔记——基础算法
  20. 千万别在朋友圈骂人,要被罚1000元!

热门文章

  1. pandoc提取word中的图片
  2. 微信小程序iphone11 wx.openBluetoothAdapter 返回状态10001 当前蓝牙适配器不可用
  3. 先验分布,后验分布,共轭分布的关系
  4. java怎么做映射_Java 映射实例
  5. espn配置路由_华为敏捷网络解决方案.ppt
  6. 网络爬虫是干什么的呢?
  7. mysql的text与tinytext_MySQL中tinytext、text、mediumtext和longtext等各个类型详解
  8. redis常用命令getex_Redis常用命令整理
  9. 三菱PLC通过CC LINK IE通讯控制2个三菱伺服的测试 程序,里面有JOG HOME,定位,适合你入门参考。包合IO规划,伺候参数,PLC程序。
  10. java使用SAXReader读取xml文件