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();
}
分享到:
相关推荐
基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj
Spring 依赖的 javax.inject.Provider
python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...
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...
microsoftrootcertificateauthority2011.cer - 教程详情https://blog.csdn.net/Dream_Weave/article/details/125408661
Android中文翻译组——Android中文API合集(4).chm
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环境中使用`...
1、android-support-v4.jar,android-support-v7-appcompat.jar,非常实用的jar文件,不可缺少,兼容Android 对应版本,包中包含类许多我们可以调用的,如widget、view、util等等。 2、导入Unity或者Android Studio...
Android support.v7包
在Android系统中,跨进程通信(IPC,Inter-Process Communication)是实现不同应用程序间数据交换的关键技术。Androidaidl(Android Interface Definition Language)是Android提供的一种接口定义语言,专门用于处理...
4. 长——短 5. 甜——苦 6. 慢——快 7. 黑——白 8. 近——远 9. 前——后 10. 冷淡——热情 11. 黑暗——光明 12. 南——北 13. 低——高 14. 闲——忙 15. 开——关 16. 哭——笑 17. 臭——香 18. 丑——美 19....
———————————————— 版权声明:本文为CSDN博主「MechMaster」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
1.深入了解Android蓝牙Bluetooth——《基础篇》 2.深入了解Android蓝牙Bluetooth——《进级篇》 2.深入了解Android蓝牙Bluetooth——《总结篇》 欢迎入群 QQ交流群 :232203809 微信公众号:终端研发部。加入我们...
16.蚁群算法的优化计算——旅行商问题(TSP)优化.zip16.蚁群算法的优化计算——旅行商问题(TSP)优化.zip16.蚁群算法的优化计算——旅行商问题(TSP)优化.zip16.蚁群算法的优化计算——旅行商问题(TSP)优化.zip16.蚁群...
实用源码——Android屏幕锁源码.zip 实用源码——Android屏幕锁源码.zip 实用源码——Android屏幕锁源码.zip 实用源码——Android屏幕锁源码.zip 实用源码——Android屏幕锁源码.zip