资源描述
Android,软件工程师系列课程,单击此处编辑母版文本样式,第二级,第三级,*,单击此处编辑母版标题样式,单击此处编辑母版文,*,Android,软件工程师系列课程,单击此处编辑母版文本样式,第二级,第三级,*,第一章,-,跨应用数据源,ContentProvider,1/21,本门课程目标,学完本门课程后,你能够:,掌握,ContentProvider,实现数据共享,使用,Service,实现,Android,应用服务,实现,Activity,与,Service,绑定和通讯,了解,BroadcastReceiver,实现广播通讯,使用,Android,四大组件实现音乐播放器,2/21,本章目标,了解,ContentProvider,作用。,使用,ContentProvider,取得系统信息(联络人),自定义,ContentProvider,应用,3/21,ContentProvider,介绍,Android,系统中存在大量应用,当不一样应用程序直接需要共享数据时,能够使用,ContentProvider,来实现。,ContentProvider,是,Android,应用四大组件之一,与,Activity,和,Serivce,相同,使用前需要注册。,当一个程序需要把自己数据暴露给其它程序使用时,该程序就能够经过提供,ContentProvider,来实现,其它应用程序就能够经过,ContenResolver,来操作,Content Provider,暴露数据。,应用程序经过,ContentProvider,开放了自己数据,该应用程序不需要开启,其它应用程序都能够操作开放数据,包含增删改查操作。,4/21,ContentProvider,使用,ContentProvider,开发步骤:,继承,Android,ContentProvider,基类实现自己,ContentProvider,类。,在,AndroidManifest.xml,文件中注册该,ContentProvider,类,为,ContentProvider,指定,Uri,。,Uri,类似网页访问中使用,URL,地址。,Uri,以,content/:,固定开头,包含注册时指定路径及资源。,为每个,Uri,指定访问时要返回数据和其它操作。,其它应用程序使用,ContenResolver,经过,ContentProvider,Uri,访问开放数据。,5/21,ContentProvider,注册,在,AndroidManifest.xml,中注册,ContentProvider,。,在,中配置,标签。,android:name,属性:,ContentProvider,类名,android:authorities,属性:用于访问,ContentProvider,名称。,6/21,ContenResolver,使用,应用程序经过,ContenProvider,开放了数据后,其它应用程序应经过,ContenResolver,访问。,ContenResolver,对象经过,Context,提供方法,getContenResolver(),来取得。,ContenResolver,提供了以下方法来操作:,insert,delete,update,query,这些方法分别会调用,ContenProvider,中与之对应方法,并得到返回结果。,7/21,使用,ContentProvider,访问手机通讯录,Android,系统提供了,Contacts,(通讯录)应用程序管理联络人。,同时系统还为“联络人管理”提供了,ContentProvider,,其它应用程序就能够经过,ContentProvider,访问联络人信息,而不用关心联络人在数据库中存放物理结构。,惯用通讯录,Uri,:,管理联络人,Uri,:,ContactsContract.Contacts.,CONTENT_URI,管理联络人电话,Uri,:,ContactsContract.CommonDataKinds.Phone.,CONTENT_URI,管理联络人,Email,Uri,:,ContactsContract.CommonDataKinds.Email.,CONTENT_URI,8/21,手机通讯录存放数据库文件,9/21,添加通讯录,ContentProvider,访问权限,使用系统资源需要在,AndroidManifest.xml,文件中添加访问权限,不然应用程序将不能使用由系统提供,ContentProvider,。,在,标签中添加:,10/21,读取联络人姓名,-1,ContentResolver.query(Uri uri,String projection,String selection,String selectionArgs,String sortOrder),方法返回一个,Cursor,对象。,uri,:,ContentProvider,uri,联络人,uri,:,ContactsContract.Contacts.,CONTENT_URI,projection,:要查询列名。,selection,:查询条件,selectionArgs,:查询条件中通配符对应值,sortOrder,:排序列,11/21,读取联络人姓名,-2,Cursor usercursor=,getContentResolver().query(,ContactsContract.Contacts.,CONTENT_URI,null,null,null,null);,SimpleCursorAdapter useradapter=,new SimpleCursorAdapter(,this,R.layout.,userlistitem,usercursor,new StringContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME,new intR.id.,txtid,R.id.txtname);,ListView lstshowname=,(ListView)findViewById(R.id.,lstshowname);,lstshowname.setAdapter(useradapter);,使用,query,方法取得联络人数据游标,创建适配器,绑定适配器,用户,ID,uri,用户名称,uri,12/21,自定义,ContentProvider,编写一个,ContentProvider,子类,该子类依据需要能够实现增、删、改、查等方法。,必须在,AndroidManifest.xml,文件中注册自定义,ContentProvider,。,创建需要开放数据,URI,,使用,UriMatcher,映射。,为,URI,关联数据,通常使用,sqlite,数据库作为数据源。,13/21,制作开放数据单词本应用,需求:,开发单词本应用,用户可添加自己学习过程中碰到英语单词并作注释,单词保留在,sqlite,数据库中。,其它应用中可依据需要访问单词本应用保留在数据库中数据。,如:某娱乐软件界面下端可随机出现一些统计生词,帮助用户在不经意中加强对单词记忆。,14/21,制作开放数据单词本应用,步骤:,开发单词本应用,设计数据库。,添加,ContentProvider,子类,开放数据库数据。,注册自定义,ContentProvider,后,编写新工程调用,ContentProvider,提供数据,15/21,单词本应用简单实现,16/21,添加,ContentProvider,public class WordStudyProvider extends ContentProvider,public static UriMatcher,matcher=new UriMatcher(UriMatcher.NO_MATCH);,static,matcher.addURI(com.demo.wordstudyprovider,wordstudy,1);,DBHelper helper;,Override,public boolean onCreate(),helper=new DBHelper(this.getContext(),wordstudydb,null,1);,return true;,Override,public Cursor query(Uri uri,String columns,String selection,String selectionArgs,String sortOrder),SQLiteDatabase db=helper.getReadableDatabase();,Cursor cursor=null;,if(,matcher.match(uri)=1),cursor=db.rawQuery(select*from wordstudy,null);,return cursor;,.,创建,UriMatcher,对象并映射,URI,在,onCreate,方法中创建数据库对象,判断,URI,,返回查询结果,Cursor,对象,17/21,注册,ContentProvider,注册,ContentProvider,Uri,使用,标签注册,18/21,新建应用,调用,contentProvider,public class WordStudyProviderTestActivity extends Activity,Override,public void onCreate(Bundle savedInstanceState),super.onCreate(savedInstanceState);,setContentView(R.layout.,main);,Cursor cursor=getContentResolver().query(,Uri.,parse(content:/com.demo.wordstudyprovider/wordstudy),null,null,null,null);,SimpleCursorAdapter adapter=new SimpleCursorAdapter(,this,R.layout.,listitem,cursor,new String_id,word_e,word_c,new intR.id.,txtid,R.id.txtworde,R.id.txtwordc);,ListView lstshow=(ListView)findViewById(R.id.,lstshow);,lstshow.setAdapter(adapter);,调用,ContentResolver,query,方法返回查询结果,Cursor,指定,contentProvider,uri,19/21,练习,1,读取联络人姓名和电话,使用,ListView,显示查询数据。,20/21,练习,2,实习单词本应用并完善。,为单词本应用开放,contentProvider,。,编写应用访问单词本,contentProvider,。,21/21,
展开阅读全文