1、自定义provider 包含两个查询:全名查询和模糊查询

Java代码
  1. packagecom.HelloWorld;
  2. importjava.io.File;
  3. importjava.io.FileOutputStream;
  4. importjava.io.IOException;
  5. importjava.io.InputStream;
  6. importandroid.content.ContentProvider;
  7. importandroid.content.ContentValues;
  8. importandroid.content.Context;
  9. importandroid.content.UriMatcher;
  10. importandroid.database.Cursor;
  11. importandroid.database.sqlite.SQLiteDatabase;
  12. importandroid.net.Uri;
  13. importandroid.os.Environment;
  14. publicclassMyProviderextendsContentProvider{
  15. privatefinalstaticStringDB_DIR="lp";
  16. privatefinalstaticStringDB_NAME="contacts.db";
  17. privatefinalstaticStringTABLE_NAME="PRMContacts";
  18. privateContextmContext;
  19. privateSQLiteDatabasedb;
  20. privatestaticfinalStringAUTHORITY="com.helloword.myprovider";
  21. privatestaticUriMatcheruriMatcher;
  22. privatestaticfinalintONE=1;
  23. privatestaticfinalintMORE=2;
  24. static
  25. {
  26. //添加访问ContentProvider的Uri
  27. uriMatcher=newUriMatcher(UriMatcher.NO_MATCH);
  28. uriMatcher.addURI(AUTHORITY,"one",ONE);
  29. uriMatcher.addURI(AUTHORITY,"more/*",MORE);
  30. }
  31. @Override
  32. publicintdelete(Uriuri,Stringselection,String[]selectionArgs){
  33. //TODOAuto-generatedmethodstub
  34. return0;
  35. }
  36. @Override
  37. publicStringgetType(Uriuri){
  38. //TODOAuto-generatedmethodstub
  39. returnnull;
  40. }
  41. @Override
  42. publicUriinsert(Uriuri,ContentValuesvalues){
  43. //TODOAuto-generatedmethodstub
  44. returnnull;
  45. }
  46. @Override
  47. publicbooleanonCreate(){
  48. mContext=getContext();
  49. db=openDatabase();
  50. returntrue;
  51. }
  52. @Override
  53. publicCursorquery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder){
  54. Cursorcursor=null;
  55. System.out.println("query");
  56. switch(uriMatcher.match(uri)){
  57. caseONE:
  58. cursor=db.query(TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
  59. break;
  60. caseMORE:
  61. Stringword=uri.getPathSegments().get(1);
  62. cursor=db.rawQuery("select*from"+TABLE_NAME+"wheredisplaynamelike?",newString[]{word+"%"});
  63. break;
  64. default:
  65. thrownewIllegalArgumentException("无效参数");
  66. }
  67. returncursor;
  68. }
  69. @Override
  70. publicintupdate(Uriuri,ContentValuesvalues,Stringselection,String[]selectionArgs){
  71. //TODOAuto-generatedmethodstub
  72. return0;
  73. }
  74. privateSQLiteDatabaseopenDatabase()
  75. {
  76. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  77. FileOutputStreamfos=null;
  78. InputStreamis=null;
  79. try
  80. {
  81. Stringpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+DB_DIR;
  82. //获得dictionary.db文件的绝对路径
  83. StringdatabaseFilename=path+"/"+DB_NAME;
  84. Filedir=newFile(path);
  85. if(!dir.exists()){
  86. dir.mkdir();
  87. }
  88. Filedb=newFile(databaseFilename);
  89. if(!db.exists()){
  90. fos=newFileOutputStream(db);
  91. is=mContext.getResources().openRawResource(R.raw.contacts);
  92. byte[]buffer=newbyte[1024];
  93. intlength=0;
  94. while((length=is.read(buffer))!=-1){
  95. fos.write(buffer,0,length);
  96. }
  97. }
  98. SQLiteDatabasedatabase=SQLiteDatabase.openOrCreateDatabase(databaseFilename,null);
  99. returndatabase;
  100. }
  101. catch(Exceptione){
  102. }finally{
  103. if(fos!=null){
  104. try{
  105. fos.close();
  106. }catch(IOExceptione){
  107. //TODOAuto-generatedcatchblock
  108. e.printStackTrace();
  109. }
  110. }
  111. if(is!=null){
  112. try{
  113. is.close();
  114. }catch(IOExceptione){
  115. //TODOAuto-generatedcatchblock
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120. }
  121. returnnull;
  122. }
  123. }


xml注册:

Java代码
  1. <receiverandroid:name=".MyBroadcast">
  2. <intent-filter>
  3. <actionandroid:name="com.lp.MyBroadcast"/>
  4. </intent-filter>
  5. </receiver>
  6. <providerandroid:name=".MyProvider"android:authorities="com.helloword.myprovider"/>


权限:

Java代码
  1. <!--在SDCard中创建与删除文件权限-->
  2. <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  3. <!--往SDCard写入数据权限-->
  4. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



2、另一个应用调用:

Java代码
  1. privateButtonfind;
  2. privateTextViewname;
  3. privatestaticfinalStringONE="content://com.helloword.myprovider/one";
  4. privatestaticfinalStringMORE="content://com.helloword.myprovider/more";
  5. find.setOnClickListener(newOnClickListener(){
  6. @Override
  7. publicvoidonClick(Viewv){
  8. Stringstr=name.getText().toString();
  9. if(str!=null){
  10. //findTelByName(str);
  11. findTelsByName(str);
  12. }
  13. }
  14. });
  15. privatevoidfindTelByName(Stringname){
  16. Uriuri=Uri.parse(ONE);
  17. Cursorcursor=getContentResolver().query(uri,null,"displayname=?",newString[]{name},null);
  18. Stringresult="没有找到电话";
  19. if(cursor!=null){
  20. cursor.moveToNext();
  21. result=cursor.getString(cursor.getColumnIndex("telnum"));
  22. }
  23. Toast.makeText(this,result,1).show();
  24. }
  25. privatevoidfindTelsByName(Stringname){
  26. Uriuri=Uri.parse(MORE+"/"+name);
  27. Cursorcursor=getContentResolver().query(uri,null,null,null,null);
  28. Stringresult="";
  29. if(cursor!=null){
  30. while(cursor.moveToNext()){
  31. result+=cursor.getString(cursor.getColumnIndex("telnum"));
  32. result+=",";
  33. }
  34. }
  35. if(!result.equals(""))
  36. Toast.makeText(this,result,1).show();
  37. }


更多相关文章

  1. 如何调用android内置图片,比如menu->add的加号图片,search的放大镜
  2. RK3288[android 7.1]调试笔记 去掉桌面上的谷歌搜索框
  3. Android(安卓)—— inflate( )使用
  4. adb下的tcpdump抓包方法
  5. qt for android 实现开机自启
  6. Android培训班(40)
  7. Android百度地图之显示地图
  8. Android系列之Android(安卓)命令行手动编译打包详解
  9. android lambda使用:Retrolambda Plugin

随机推荐

  1. Android jni系统变量、函数、接口定义汇
  2. Android使用ADB启动应用程序
  3. Android换肤白天/夜间模式的框架
  4. Android 应用的真正入口 - ZygoteInit.ja
  5. android 摄像头(我想在类里面直接编辑UI界
  6. Gradle in Android Studio: Failed to re
  7. 使用GDB调试Android 4.0中的WebKit
  8. Android系统Home应用程序(Launcher)的启动
  9. Android下实现一个手机监控摄像头
  10. 两分钟彻底让你明白Android Activity生命