某500强android学习资料-第六章-Android进程间通信_第1页
某500强android学习资料-第六章-Android进程间通信_第2页
某500强android学习资料-第六章-Android进程间通信_第3页
某500强android学习资料-第六章-Android进程间通信_第4页
某500强android学习资料-第六章-Android进程间通信_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六章Android进程间通信der便,制,对于库和框架层的开发人员来说也会更多的用到底层的机制,这些内容会放在另一篇d6.1BroadcastReceiver在android中,通过广播(broadcast)可以通知其他广播接受者某个事件发生了。比如电源publicclassBroadcastTestextendsActivity{publicstaticfinalStringNEW_LIFEFROM_DETECTED=“com.android.broadcasttest.NEW_LIFEFROM”;publicvoidonCreate(BundlesavedInstanceState){ButtonbtnButtonfindViewByIdRid.btn0);btn0.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){ewIntentNEWLIFEFROMDETECTED}}…}PublicclassMyBroadcastReceiverextendsBroadcastReceiver{voidonReceiveContextcontextInteneintent}}eceiverandroidnameMyBroadcastReceiverntfilter<actionandroid:name=”com.android.broadcasttest.NEW_LIFEFROM”/>entfiltereroadcastBroadcastReceiver->通知Broadcast->启动相关处理应用。比如,监听来电、邮件、短信之类的,都隶oadcastReceivermReceivernewBroadcastReceivercvoidonReceiveContextcontextIntentintent}}ilternewIntentFiltermfilteraddActionIntentACTION_SCREEN_OFF);ivermReceivermfiltersterReceivermReceiverctivityManagerServicejavapandroidappdocsappspotcomreferenceandroidcontentIntenthtmlBroadcastr6.2IntentIntent是一种运行时绑定(run-timebinding)机制,它能在程序运行过程中连接两个不同的要激活一个新的Activity,或者让一个现有的Activity做新的操作,可以通过调用Context.startService()方法或者调用Context.bindService()方法将调用此方法的上下文对三个方法可以发送BroadcastIntent。发送之后,所有已注册的并且拥有与之相匹配Intent一旦发出,Android都会准确找到相匹配的一个或多个Activity,Service或者tntCTIONCALL启动一个电话.CTIONEDIT显示用户编辑的数据.ACTIONMAINIONSYNCHEADSETPLUG警告ACTIONSCREEN_ON屏幕变亮警告.ONTIMEZONECHANGED改变时区警告.UriuriUriparse"");newIntentIntentACTIONVIEWurity该activity可以被包含在另外一个装载小工具的activity中程序启动器中Type:显式指定Intent的数据类型(MIME)(多用途互联网邮件扩展,Multipurposenttypecontent://com.example.transportationprovider/trains/122(一条列车信息的uri)的多个记录的格式:vnd.android.cursor.dir/vnd.yourcompanyname.contenttype,如:contentcomexampletransportationprovidertrains(所有列车信息)的MIME类itnewIntentActivityMainthisActivityclassBundleputExtras)和getExtras()作为Bundle来读和写。例如:itnewIntentActivityMainthisActivityclassndlebundlenewBundlebundleputStringname,"ThisisfromMainActivity!");ndleBundlebundlegetIntentgetExtrasebundlegetStringnamesetClassContextClass件。是那些间接Intent,通过解析将Intent映射给可以处理此Intent的Activity、Service或estxmlintentfilterIntentcategory现在组建的类别列表中。标组件必须至少包含这两个类别。httpwwwcnblogscomphinecosarchive09/08/26/1554684.htmlfestxml<manifestxmlns:android="/apk/res/android"package="com.example.android.notepad">applicationandroidicondrawableappnotesoidlabelstringappnameoviderandroidnameNotePadProviderandroid:authorities="vider.NotePad"activityandroidnameNotesListandroidlabel"@string/title_notes_list">ntfilter<actionandroid:name="ent.action.MAIN"/><categoryandroid:name="ent.category.LAUNCHER"/>entfilterntfilter<actionandroid:name="ent.action.VIEW"/><actionandroid:name="ent.action.EDIT"/><actionandroid:name="ent.action.PICK"/><categoryandroid:name="ent.category.DEFAULT"/><dataandroid:mimeType="vnd.android.cursor.dir/vnd.google.note"/>entfilterntfilter<actionandroid:name="ent.action.GET_CONTENT"/><categoryandroid:name="ent.category.DEFAULT"/><dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"/>entfilterty选择(android.app.action.PICK)。erivityandroidnameNoteEditordroidthemeandroidstyleThemeLightbelstringtitlenotenOrientationsensor>intentfilterandroidlabelstringresolve_edit"><actionandroid:name="ent.action.VIEW"/><actionandroid:name="ent.action.EDIT"/><actionandroid:name="com.android.notepad.action.EDIT_NOTE"/><categoryandroid:name="ent.category.DEFAULT"/><dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"/>entfilternotes.-->ntfilter<actionandroid:name="ent.action.INSERT"/><categoryandroid:name="ent.category.DEFAULT"/><dataandroid:mimeType="vnd.android.cursor.dir/vnd.google.note"/>entfilterty第一个filter查看、编辑便笺第二个filter新建便笺IVENamectionmponentNameIntentIntentintMenuItemivityandroidnameTitleEditorndroidlabelstringtitleedittitledroidthemeandroidstyleThemeDialogwindowSoftInputModestateVisibleintentfilterandroidlabel@string/resolve_title"><actionandroid:name="com.android.notepad.action.EDIT_TITLE"/><categoryandroid:name="ent.category.DEFAULT"/><categoryandroid:name="ent.category.ALTERNATIVE"/><dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"/>entfiltertyintentfilterandroidlabel="@string/resolve_title"指定),yomgoogleandroidnotepadactionEDITTITLEtacontentcomgoogleproviderNotePadnotesIDandroidcomreferenceandroidcontentintenthtml6.3ContentProviderider一组用于提供其他应用程序存取数据的标准方法的类。er查询数据修改数据添加数据删除数据 “content://”代表数据的路径一个可选的标识数据的IDentcontactspeopleURIcontent://contacts/people/45这个URI返回单个结果(ID为45的联系人记录)Android提供了一系列的帮助类(在vider包下),里面包含了很多以类变量形式MediaStoreImagesMediaINTERNAL_CONTENT_URIeopleCONTENTURIUriperson=ContentUriswithAppendedId(PeopleCONTENTURI询:CursorcurmanagedQuerypersonnullnullnull);publicclassContentProviderDemoextendsActivity{publicvoidonCreate(BundlesavedInstanceState){vedInstanceStateRlayoutmainecords}privatevoiddisplayRecords(){Stringcolumns[]=newString[]{People.NAME,People.NUMBER};rimContactsPeopleCONTENTURIurmanagedQueryContactsif(cur.moveToFirst()){nulleNonulldo{namecurgetStringcurgetColumnIndexPeopleNAMEphoneNocurgetStringcur.getColumnIndex(People.NUMBER));extthisnamephoneNo,THLONG}}}更改第10条记录的name字段值为“XYZ”privatevoidupdateRecord(intrecNo,Stringname){UriuriContentUriswithAppendedIdPeople.CONTENT_URI,recNo);entValuesvaluesnewContentValuesvaluesputPeopleNAMEname);getContentResolverupdateurivaluesnullnull;}privatevoidinsertRecords(Stringname,StringphoneNo){entValuesvaluesnewContentValuesvaluesputPeopleNAMEname);UriurigetContentResolverinsertPeople.CONTENT_URI,values);Log.d(”ANDROID”,uri.toString());YvaluesclearvaluesputContactsPhonesTYPEPeoplePhones.TYPE_MOBILE);valuesputPeopleNUMBERphoneNo;ontentResolverinsertnumberUrivalues}这样我们就可以调用insertRecords(name,phoneNo)的方式来向联系人信息簿中添加联系人号码。privatevoiddeleteRecords(){riuriPeopleCONTENTURIetContentResolverdeleteurinullnull}getContentResolver()deleteuriNAMEXYZ‟”,null);好是类的全名称,如ticfinalUriCONTENTURIUriparsecontentcommycomMyContentProvider实rCursor方法如insert(),update()以及gerpublicclassMyUsers{publicstaticfinalStringAUTHORITY“com.wissen.MyContentProvider”;publicstaticfinalclassUserimplementsBaseColumns{publicstaticfinalUriCONTENT_URI=UriparsecontentcomwissenMyContentProvider);publicstaticfinalStringUSERNAME=“USER_NAME”;}}publicclassMyContentProviderextendsContentProvider{privateSQLiteDatabasesqlDB;rivateDatabaseHelperdbHelperprivatestaticfinalStringDATABASE_NAME=“Users.db”;privatestaticfinalintDATABASE_VERSION=1;privatestaticfinalStringTABLE_NAME=“User”;privatestaticfinalStringTAG=“MyContentProvider”;privatestaticfinalStringDATABASE_CREATE=eatetableTABLENAMEerprimarykeyautoincrementUSERNAMEtexterprivatestaticclassDatabaseHelperextendsSQLiteOpenHelper{DatabaseHelper(Contextcontext){extDATABASENAMEnullDATABASEVERSION}edbSECREATE}{dbexecSQLDROPTABLEIFEXISTSTABLENAME;}}publicbooleanonCreate(){elpernewDatabaseHelpergetContextreturndbHelpernullfalsetrue}publicUriinsert(Uriuri,ContentValuescontentvalues){rgetWritableDatabasengrowIdsqlDBinsertTABLENAMEcontentvaluesif(rowId>0){UrirowUri=tentUrisappendIdMyUsersUserCONTENTURIbuildUpongetContextgetContentResolvernotifyChangerowUrinull}thrownewSQLExceptionFailedtoinsertrowintouri);}selectionArgs,StringsortOrder){ryBuilderqbnewSQLiteQueryBuilderabasedbdbHelpergetReadableDatabaseblesTABLENAMECursorcqbquerydbprojectionselection,ullnullnullsortOrderNotificationUrigetContextgetContentRes

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论