2011.09.07(4)——— android 跨进程通信之content provider
参考:
http://blog.csdn.net/yan8024/article/details/6444368
1、自定义provider 包含两个查询:全名查询和模糊查询
package com.HelloWorld;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
public class MyProvider extends ContentProvider {
private final static String DB_DIR = "lp";
private final static String DB_NAME = "contacts.db";
private final static String TABLE_NAME = "PRMContacts";
private Context mContext;
private SQLiteDatabase db ;
private static final String AUTHORITY = "com.helloword.myprovider" ;
private static UriMatcher uriMatcher;
private static final int ONE = 1 ;
private static final int MORE = 2 ;
static
{
// 添加访问ContentProvider的Uri
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "one" , ONE);
uriMatcher.addURI(AUTHORITY, "more/*" , MORE);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
mContext = getContext();
db = openDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor = null;
System.out.println("query");
switch(uriMatcher.match(uri)){
case ONE:
cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
case MORE:
String word = uri.getPathSegments().get(1);
cursor = db.rawQuery("select * from "+TABLE_NAME+" where displayname like ?", new String[]{word+"%"});
break;
default:
throw new IllegalArgumentException("无效参数");
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
private SQLiteDatabase openDatabase()
{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileOutputStream fos = null;
InputStream is = null;
try
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+DB_DIR;
// 获得dictionary.db文件的绝对路径
String databaseFilename = path + "/" + DB_NAME;
File dir = new File(path);
if(!dir.exists()){
dir.mkdir();
}
File db = new File(databaseFilename);
if(!db.exists()){
fos = new FileOutputStream(db);
is = mContext.getResources().openRawResource(R.raw.contacts);
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer))!=-1){
fos.write(buffer, 0, length);
}
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
return database;
}
catch (Exception e){
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
}
xml注册:
<receiver android:name=".MyBroadcast" >
<intent-filter>
<action android:name="com.lp.MyBroadcast"/>
</intent-filter>
</receiver>
<provider android:name=".MyProvider" android:authorities="com.helloword.myprovider" />
权限:
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2、另一个应用调用:
private Button find;
private TextView name;
private static final String ONE = "content://com.helloword.myprovider/one" ;
private static final String MORE = "content://com.helloword.myprovider/more" ;
find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = name.getText().toString();
if(str!=null){
//findTelByName(str);
findTelsByName(str);
}
}
});
private void findTelByName(String name){
Uri uri = Uri.parse(ONE);
Cursor cursor = getContentResolver().query(uri, null, "displayname=?", new String[]{name}, null);
String result = "没有找到电话";
if(cursor!=null){
cursor.moveToNext();
result = cursor.getString(cursor.getColumnIndex("telnum"));
}
Toast.makeText(this, result, 1).show();
}
private void findTelsByName(String name){
Uri uri = Uri.parse(MORE+"/"+name);
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String result = "";
if(cursor!=null){
while(cursor.moveToNext()){
result += cursor.getString(cursor.getColumnIndex("telnum"));
result += ",";
}
}
if(!result.equals(""))
Toast.makeText(this, result, 1).show();
}
分享到:
相关推荐
Android跨进程跨app通信框架魔法信使Android跨进程跨app通信框架本框架实现了Android跨进程跨app通信的方便调用和封装使用方法第一步:在应用程序里面初始化MagicMessenger.init(this); //跨进程需要MagicMessenger....
基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj
python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...
2020.4.11-措施项目——模板工程量计算规则
在Android系统中,跨进程通信(IPC,Inter-Process Communication)是必不可少的,尤其是在开发大型、复杂的应用程序时。Android Interface Definition Language(AIDL)就是Android为开发者提供的一种强大的工具,...
python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ...
微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip 微信小程序——新浪读书(截图+源码).zip ...
数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip数据库大作业——学校人事信息管理系统.zip...
C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统.zip C语言项目——企业员工管理系统....
`sunjce_provider.jar`是Java平台的标准加密提供者之一,由Sun Microsystems开发,包含了许多加密算法实现,如RSA、AES、DES等。这个压缩包`JAVA数据加密之sunjce_provider.rar`显然提供了关于如何在Java环境中使用`...
微信小程序——仿微信DEMO(截图+源码).zip 微信小程序——仿微信DEMO(截图+源码).zip 微信小程序——仿微信DEMO(截图+源码).zip 微信小程序——仿微信DEMO(截图+源码).zip 微信小程序——仿微信DEMO(截图+...
城市规划毕业设计——尚上人家景观设计(毕业论文+答辩PPT+CAD图+设计图+效果图).zip 城市规划毕业设计——尚上人家景观设计(毕业论文+答辩PPT+CAD图+设计图+效果图).zip 城市规划毕业设计——尚上人家景观设计...
1、android-support-v4.jar,android-support-v7-appcompat.jar,非常实用的jar文件,不可缺少,兼容Android 对应版本,包中包含类许多我们可以调用的,如widget、view、util等等。 2、导入Unity或者Android Studio...
4. 长——短 5. 甜——苦 6. 慢——快 7. 黑——白 8. 近——远 9. 前——后 10. 冷淡——热情 11. 黑暗——光明 12. 南——北 13. 低——高 14. 闲——忙 15. 开——关 16. 哭——笑 17. 臭——香 18. 丑——美 19....
而`Marvell_88NV1120_MPTool_2019.07.25_Toshiba`则是工具的执行程序,用户需要按照readme中的步骤进行操作。 在实际应用中,Marvell 88NV1120 MPTool不仅适用于数据中心和企业环境,也适合对存储性能有高要求的...
p102025301120——Linux-x86-64_4of7.zip client安装介质 p102025301120——Linux-x86-64_5of7.zip gateways安装介质 p102025301120——Linux-x86-64_6of7.zip example p102025301120——Linux-x86-64_7of7.zip ...
———————————————— 版权声明:本文为CSDN博主「MechMaster」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
1.深入了解Android蓝牙Bluetooth——《基础篇》 2.深入了解Android蓝牙Bluetooth——《进级篇》 2.深入了解Android蓝牙Bluetooth——《总结篇》 欢迎入群 QQ交流群 :232203809 微信公众号:终端研发部。加入我们...
java毕业设计——基于spring boot的音乐播放网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的音乐播放网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的音乐播放网站设计与...
Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA,提供了集成的 Android 开发工具用于开发和调试,该压缩文件包含了android-studio-2020.3.1.24-windows.exe和下载.txt,欢迎下载。