CSipSimple是运行在android设备上的一个开源的sip协议应用程序,本文其中的拨打电话机制进行大致分析。
项目中,拨打电话利用了AIDL方法来实现。aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它来定义进程间的通信接口,完成IPC(Inter-Process Communication,进程间通信)。
创建.aidl文件
ISipService.aidl内容如下:
/** * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr) * This file is part of CSipSimple. * * CSipSimple is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * If you own a pjsip commercial license you can also redistribute it * and/or modify it under the terms of the GNU Lesser General Public License * as an android library. * * CSipSimple is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CSipSimple. If not, see <http://www.gnu.org/licenses/>. * * This file and this file only is also released under Apache license as an API file */ package com.csipsimple.api; import com.csipsimple.api.SipProfileState; import com.csipsimple.api.SipCallSession; import com.csipsimple.api.MediaState; interface ISipService{ /** * Get the current API version * @return version number. 1000 x major version + minor version * Each major version must be compatible with all versions of the same major version */ ......... void makeCallWithOptions(in String callee, int accountId, in Bundle options); }ISipService.aidl中定义了包含makeCallWithOptions
方法的接口ISipService。
自动编译生成java文件
eclipse中的ADT插件会自动在aidl文件中声明的包名目录下生成java文件,如下图所示:
ISipService.java
package com.csipsimple.api; public interface ISipService extends android.os.IInterface { …… //Place a call public void makeCallWithOptions(java.lang.String callee, int accountId, android.os.Bundle options) throws android.os.RemoteException; }接下来就是实现ISipService.aidl中定义的接口,提供接口的实例供客户端调用
IPC实现 项目中拨打电话
void
com.csipsimple.api.ISipService.makeCallWithOptions(String
msg, String toNumber, long accountId)
结合代码一层层看调用
目录:src\com\csipsimple\ui\dialpad
DialerFragment.java
private ISipService service; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { service = ISipService.Stub.asInterface(arg1); ........ } @Override public void onServiceDisconnected(ComponentName arg0) { service = null; } };
@Override public void placeCall() { placeCallWithOption(null); } private void placeCallWithOption(Bundle b) { if (service == null) { return; } String toCall = ""; Long accountToUse = SipProfile.INVALID_ID; // Find account to use SipProfile acc = accountChooserButton.getSelectedAccount(); if (acc != null) { accountToUse = acc.id; } // Find number to dial if(isDigit) { toCall = PhoneNumberUtils.stripSeparators(digits.getText().toString()); }else { toCall = digits.getText().toString(); } if (TextUtils.isEmpty(toCall)) { return; } // Well we have now the fields, clear theses fields digits.getText().clear(); // -- MAKE THE CALL --// if (accountToUse >= 0) { // It is a SIP account, try to call service for that try { service.makeCallWithOptions(toCall, accountToUse.intValue(), b); } catch (RemoteException e) { Log.e(THIS_FILE, "Service can't be called to make the call"); } } else if (accountToUse != SipProfile.INVALID_ID) { // It's an external account, find correct external account CallHandlerPlugin ch = new CallHandlerPlugin(getActivity()); ch.loadFrom(accountToUse, toCall, new OnLoadListener() { @Override public void onLoad(CallHandlerPlugin ch) { placePluginCall(ch); } }); } }
2.服务端 SipService.java /** * 继承 Service发布服务 */ public class SipService extends Service { ... // 为服务实现公共接口, Stub类继承了Binder private final ISipService.Stub binder = new ISipService.Stub() { ... @Override public void makeCallWithOptions(final String callee, final int accountId, final Bundle options) throws RemoteException { SipService.this.enforceCallingOrSelfPermission(SipManager.PERMISSION_USE_SIP, null); //We have to ensure service is properly started and not just binded SipService.this.startService(new Intent(SipService.this, SipService.class)); if(pjService == null) { Log.e(THIS_FILE, "Can't place call if service not started"); // TODO - we should return a failing status here return; } if(!supportMultipleCalls) { // Check if there is no ongoing calls if so drop this request by alerting user SipCallSession activeCall = pjService.getActiveCallInProgress(); if(activeCall != null) { if(!CustomDistribution.forceNoMultipleCalls()) { notifyUserOfMessage(R.string.not_configured_multiple_calls); } return; } } getExecutor().execute(new SipRunnable() { @Override protected void doRun() throws SameThreadException { pjService.makeCall(callee, accountId, options); } }); }
/**
* 返回一个实现了接口的类对象,给客户端接收
*/
@Override
public IBinder onBind(Intent intent) {
String serviceName = intent.getAction();
Log.d(THIS_FILE, "Action is " + serviceName );
if (serviceName == null || serviceName.equalsIgnoreCase(SipManager.INTENT_SIP_SERVICE )) {
Log.d(THIS_FILE, "Service returned");
return binder ;
} else if (serviceName. equalsIgnoreCase(SipManager.INTENT_SIP_CONFIGURATION )) {
Log.d(THIS_FILE, "Conf returned");
return binderConfiguration ;
}
Log.d(THIS_FILE, "Default service (SipService) returned");
return binder;
}
...
}
就可以被其他Activity访问调用了。
service.makeCallWithOptions(toCall, accountToUse.intValue(), b);调用接口中的方法,完成IPC方法。
public int makeCall(String callee, int accountId, Bundle b) throws SameThreadException { if (!created) { return -1; } final ToCall toCall = sanitizeSipUri(callee, accountId); if (toCall != null) { pj_str_t uri = pjsua.pj_str_copy(toCall.getCallee()); // Nothing to do with this values byte[] userData = new byte[1]; int[] callId = new int[1]; pjsua_call_setting cs = new pjsua_call_setting(); pjsua_msg_data msgData = new pjsua_msg_data(); int pjsuaAccId = toCall.getPjsipAccountId(); // Call settings to add video pjsua.call_setting_default(cs); cs.setAud_cnt(1); cs.setVid_cnt(0); if(b != null && b.getBoolean(SipCallSession.OPT_CALL_VIDEO, false)) { cs.setVid_cnt(1); } cs.setFlag(0); pj_pool_t pool = pjsua.pool_create("call_tmp", 512, 512); // Msg data to add headers pjsua.msg_data_init(msgData); pjsua.csipsimple_init_acc_msg_data(pool, pjsuaAccId, msgData); if(b != null) { Bundle extraHeaders = b.getBundle(SipCallSession.OPT_CALL_EXTRA_HEADERS); if(extraHeaders != null) { for(String key : extraHeaders.keySet()) { try { String value = extraHeaders.getString(key); if(!TextUtils.isEmpty(value)) { int res = pjsua.csipsimple_msg_data_add_string_hdr(pool, msgData, pjsua.pj_str_copy(key), pjsua.pj_str_copy(value)); if(res == pjsuaConstants.PJ_SUCCESS) { Log.e(THIS_FILE, "Failed to add Xtra hdr (" + key + " : " + value + ") probably not X- header"); } } }catch(Exception e) { Log.e(THIS_FILE, "Invalid header value for key : " + key); } } } } int status = pjsua.call_make_call(pjsuaAccId, uri, cs, userData, msgData, callId); if(status == pjsuaConstants.PJ_SUCCESS) { dtmfToAutoSend.put(callId[0], toCall.getDtmf()); Log.d(THIS_FILE, "DTMF - Store for " + callId[0] + " - "+toCall.getDtmf()); } pjsua.pj_pool_release(pool); return status; } else { service.notifyUserOfMessage(service.getString(R.string.invalid_sip_uri) + " : " + callee); } return -1; }
package org.pjsip.pjsua; public class pjsua implements pjsuaConstants { public synchronized static int call_make_call(int acc_id, pj_str_t dst_uri, pjsua_call_setting opt, byte[] user_data, pjsua_msg_data msg_data, int[] p_call_id) { return pjsuaJNI.call_make_call(acc_id, pj_str_t.getCPtr(dst_uri), dst_uri, pjsua_call_setting.getCPtr(opt), opt, user_data, pjsua_msg_data.getCPtr(msg_data), msg_data, p_call_id); } .......... }
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.4
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------- */
package org.pjsip.pjsua;
public class pjsuaJNI {
...
public final static native int call_make_call(int jarg1, long jarg2, pj_str_t jarg2_, long jarg3, pjsua_call_setting jarg3_, byte[] jarg4, long jarg5, pjsua_msg_data jarg5_, int[] jarg6);
...
}
我们看到了native方法call_make_call,它调用的是封装在库libpjsipjni.so中的函数pjsua_call_make_call,进一步可以在jni目录下找到C代码。
PJ_DEF(pj_status_t) pjsua_call_make_call(pjsua_acc_id acc_id, const pj_str_t *dest_uri, const pjsua_call_setting *opt, void *user_data, const pjsua_msg_data *msg_data, pjsua_call_id *p_call_id) { pj_pool_t *tmp_pool = NULL; pjsip_dialog *dlg = NULL; pjsua_acc *acc; pjsua_call *call; int call_id = -1; pj_str_t contact; pj_status_t status; /* Check that account is valid */ PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), PJ_EINVAL); /* Check arguments */ PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL); PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id, (int)dest_uri->slen, dest_uri->ptr)); pj_log_push_indent(); PJSUA_LOCK(); /* Create sound port if none is instantiated, to check if sound device * can be used. But only do this with the conference bridge, as with * audio switchboard (i.e. APS-Direct), we can only open the sound * device once the correct format has been known */ if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && !pjsua_var.no_snd) { status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); if (status != PJ_SUCCESS) goto on_error; } acc = &pjsua_var.acc[acc_id]; if (!acc->valid) { pjsua_perror(THIS_FILE, "Unable to make call because account " "is not valid", PJ_EINVALIDOP); status = PJ_EINVALIDOP; goto on_error; } /* Find free call slot. */ call_id = alloc_call_id(); if (call_id == PJSUA_INVALID_ID) { pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY); status = PJ_ETOOMANY; goto on_error; } call = &pjsua_var.calls[call_id]; /* Associate session with account */ call->acc_id = acc_id; call->call_hold_type = acc->cfg.call_hold_type; /* Apply call setting */ status = apply_call_setting(call, opt, NULL); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Failed to apply call setting", status); goto on_error; } /* Create temporary pool */ tmp_pool = pjsua_pool_create("tmpcall10", 512, 256); /* Verify that destination URI is valid before calling * pjsua_acc_create_uac_contact, or otherwise there * a misleading "Invalid Contact URI" error will be printed * when pjsua_acc_create_uac_contact() fails. */ if (1) { pjsip_uri *uri; pj_str_t dup; pj_strdup_with_null(tmp_pool, &dup, dest_uri); uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0); if (uri == NULL) { pjsua_perror(THIS_FILE, "Unable to make call", PJSIP_EINVALIDREQURI); status = PJSIP_EINVALIDREQURI; goto on_error; } } /* Mark call start time. */ pj_gettimeofday(&call->start_time); /* Reset first response time */ call->res_time.sec = 0; /* Create suitable Contact header unless a Contact header has been * set in the account. */ if (acc->contact.slen) { contact = acc->contact; } else { status = pjsua_acc_create_uac_contact(tmp_pool, &contact, acc_id, dest_uri); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); goto on_error; } } /* Create outgoing dialog: */ status = pjsip_dlg_create_uac( pjsip_ua_instance(), &acc->cfg.id, &contact, dest_uri, dest_uri, &dlg); if (status != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Dialog creation failed", status); goto on_error; } /* Increment the dialog's lock otherwise when invite session creation * fails the dialog will be destroyed prematurely. */ pjsip_dlg_inc_lock(dlg); if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp); /* Calculate call's secure level */ call->secure_level = get_secure_level(acc_id, dest_uri); /* Attach user data */ call->user_data = user_data; /* Store variables required for the callback after the async * media transport creation is completed. */ if (msg_data) { call->async_call.call_var.out_call.msg_data = pjsua_msg_data_clone( dlg->pool, msg_data); } call->async_call.dlg = dlg; /* Temporarily increment dialog session. Without this, dialog will be * prematurely destroyed if dec_lock() is called on the dialog before * the invite session is created. */ pjsip_dlg_inc_session(dlg, &pjsua_var.mod); /* Init media channel */ status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, call->secure_level, dlg->pool, NULL, NULL, PJ_TRUE, &on_make_call_med_tp_complete); if (status == PJ_SUCCESS) { status = on_make_call_med_tp_complete(call->index, NULL); if (status != PJ_SUCCESS) goto on_error; } else if (status != PJ_EPENDING) { pjsua_perror(THIS_FILE, "Error initializing media channel", status); pjsip_dlg_dec_session(dlg, &pjsua_var.mod); goto on_error; } /* Done. */ if (p_call_id) *p_call_id = call_id; pjsip_dlg_dec_lock(dlg); pj_pool_release(tmp_pool); PJSUA_UNLOCK(); pj_log_pop_indent(); return PJ_SUCCESS; on_error: if (dlg) { /* This may destroy the dialog */ pjsip_dlg_dec_lock(dlg); } if (call_id != -1) { reset_call(call_id); pjsua_media_channel_deinit(call_id); } if (tmp_pool) pj_pool_release(tmp_pool); PJSUA_UNLOCK(); pj_log_pop_indent(); return status; }
通过本文的研究分析,我们了解到CSipSimple通过aidl方法实现进程间通信,从而实现了拨打电话功能。
原问题来自于CSDN问答频道,更多见:http://ask.csdn.net/questions/1679
问题描述:
我使用 arrayadapter 来 filter 一个 listview。Arraydapter 的参数是一个String[][]。但是没有成功,必须要重写 Filter interface吗?要是这样的话,如何重写呢?
filter 数组的每一个位置:
galleryValues[0][0] -> "tiulo" [0][1] -> "desc" [0][2] -> "etc" lstContinente = (ListView)findViewById(R.id.list); lstContinente.setTextFilterEnabled(true); adapter = new PortaitArrayAdapter(cx,galleryValues); lstContinente.setAdapter(adapter); ed_busqueda.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(s.toString()); adapter.notifyDataSetChanged(); } });
The adapter code:
public class PortaitArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[][] values; private List<Imagen> imagenes = null; private LayoutInflater mInflater; public ImageLoader imageLoader; public PortaitArrayAdapter(Context context, String[][] values) { super(context, R.layout.gallery_row); this.context = context; this.values = values; mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); imagenes = new ArrayList<Imagen>(); for (int i = 0; i < 20; i++) imagenes.add(new Imagen()); Bitmap def = BitmapFactory.decodeResource(this.context.getResources(),R.drawable.ic_launcher); imageLoader=new ImageLoader(this.context,def, imagenes); } @Override public int getCount (){ return this.values.length; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.gallery_row, parent, false); holder.txtTitulo = (TextView) convertView.findViewById(R.id.txt_gallery_titulo); holder.txtDesc = (TextView) convertView.findViewById(R.id.txt_gallery_desc); holder.txtFecha = (TextView) convertView.findViewById(R.id.txt_gallery_fecha); holder.txtEst = (TextView) convertView.findViewById(R.id.txt_gallery_est); holder.imageView = (ImageView)convertView.findViewById(R.id.lst_img_gallery); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } /*LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.gallery_row, parent, false);*/ //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); Bitmap bmp; Log.v("Position --> ",String.valueOf(position)); try { byte b[] = imagenes.get(position).getImageData(); if (b != null) { bmp = BitmapFactory.decodeByteArray(b, 0, b.length); if (bmp != null) holder.imageView.setImageBitmap(bmp); } else { String urlBase = galleryValues[position][0].substring(0, galleryValues[position][0].lastIndexOf("/")+1); String urlToEncode = galleryValues[position][0].substring(galleryValues[position][0].lastIndexOf("/")+1, galleryValues[position][0].length()); urlToEncode = URLEncoder.encode(urlToEncode,"UTF-8"); String url = urlBase.concat(urlToEncode); url = url.replace("+", "%20"); Log.v("UrlFinal --> ",url); imageLoader.DisplayImage(String.valueOf(position),url,act,holder.imageView, position,null); } } catch (Exception e) { Log.e(this.getClass().getName(),"Exception en pos = " + position + " error:" + e.getMessage()); e.printStackTrace(); } holder.txtTitulo.setText(galleryValues[position][1] + ", " + galleryValues[position][2]); String[] dates = galleryValues[position][4].split("/"); String date = dates [1] + "/" + dates[0] + "/" + dates[2]; Date d1 = new Date(date); DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); holder.txtDesc.setText(galleryValues[position][3]); holder.txtFecha.setText(df.format(d1)); holder.txtEst.setText(getText(R.string.num_fotos_gallery) + galleryValues[position][5] + " - " + getText(R.string.num_videos_gallery) + galleryValues[position][6] + " - " + getText(R.string.num_exp_gallery) + galleryValues[position][7]); return convertView; } } private static class ViewHolder { TextView txtTitulo; TextView txtDesc; TextView txtFecha; TextView txtEst; ImageView imageView; }
:
把字符串数组放在 ArrayList 中,然后把它传递到 Adapter。使用下面的代码或者用 String[]更改下面的代码。
需要在 Adapter 类中实现 Filterable ,重写 getFilter()。
参考下面的代码关于 Filtering custom Adapter.
public class ListFilterActivity extends ListActivity { private List<String> list = new ArrayList<String>(); List<String> mOriginalValues; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final MyAdapter adapter = new MyAdapter(this, getModel()); setListAdapter(adapter); EditText filterEditText = (EditText) findViewById(R.id.filterText); // Add Text Change Listener to EditText filterEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // Call back the Adapter with current character to Filter adapter.getFilter().filter(s.toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { } @Override public void afterTextChanged(Editable s) { } }); } private List<String> getModel() { list.add("Linux"); list.add("Windows7"); list.add("Suse"); list.add("Eclipse"); list.add("Ubuntu"); list.add("Solaris"); list.add("Android"); list.add("iPhone"); list.add("Windows XP"); return list; } // Adapter Class public class MyAdapter extends BaseAdapter implements Filterable { List<String> arrayList; List<String> mOriginalValues; // Original Values LayoutInflater inflater; public MyAdapter(Context context, List<String> arrayList) { this.arrayList = arrayList; inflater = LayoutInflater.from(context); } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } private class ViewHolder { TextView textView; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.row, null); holder.textView = (TextView) convertView .findViewById(R.id.textview); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(arrayList.get(position)); return convertView; } @Override public Filter getFilter() { Filter filter = new Filter() { @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint,FilterResults results) { arrayList = (List<String>) results.values; // has the filtered values notifyDataSetChanged(); // notifies the data with new filtered values } @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values List<String> FilteredArrList = new ArrayList<String>(); if (mOriginalValues == null) { mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues } /******** * * If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values * else does the Filtering and returns FilteredArrList(Filtered) * ********/ if (constraint == null || constraint.length() == 0) { // set the Original result to return results.count = mOriginalValues.size(); results.values = mOriginalValues; } else { constraint = constraint.toString().toLowerCase(); for (int i = 0; i < mOriginalValues.size(); i++) { String data = mOriginalValues.get(i); if (data.toLowerCase().startsWith(constraint.toString())) { FilteredArrList.add(data); } } // set the Filtered result to return results.count = FilteredArrList.size(); results.values = FilteredArrList; } return results; } }; return filter; } } }
创建Contentprovider:
1. 创建一个provider----ExampleContentProvider
- 设计Content URIs(
a. 设计authority b. 设计path c.处理content URI IDs d.Content URI patterns
) - 实现Provider中必须的方法( query()insert()update()delete()getType()onCreate() )
- 定义MIME Types( getType()One of the required methods that you must implement for any provider. getStreamTypes()A method that you're expected to implement if your provider offers files.)
package com.hualu.contentprovider; import java.util.HashMap; import java.util.Map; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; public class ExampleContentProvider extends ContentProvider { /* * Defines a handle to the database helper object. The MainDatabaseHelper class is defined * in a following snippet. */ private MainDatabaseHelper mOpenHelper; // Defines the database name private static final String DBNAME = "mydb"; private static final int MAINS = 1 ; private static final int MAIN_ID = 2 ; /** * A UriMatcher instance */ private static final UriMatcher sUriMatcher; private static Map<String, String > columnMap = new HashMap<String, String>() ; static{ // Create a new instance sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(Main.AUTHORITY, "mains", MAINS) ; sUriMatcher.addURI(Main.AUTHORITY, "main", MAIN_ID) ; sUriMatcher.addURI(Main.AUTHORITY, "main/#", MAIN_ID) ; columnMap.put("id","_ID") ; columnMap.put("word","WORD") ; } public boolean onCreate() { /* * Creates a new helper object. This method always returns quickly. * Notice that the database itself isn't created or opened * until SQLiteOpenHelper.getWritableDatabase is called */ mOpenHelper = new MainDatabaseHelper( getContext() // the application context ); return true; } @Override public int delete(Uri arg0, String arg1, String[] arg2) { return 0; } @Override public String getType(Uri uri) { switch (sUriMatcher.match(uri)) { case MAINS:{ return Main.CONTENT_TYPE; } case MAIN_ID:{ return Main.CONTENT_ITEM_TYPE; } } return null; } @Override public Uri insert(Uri uri, ContentValues values) { if(sUriMatcher.match(uri) == MAIN_ID){ throw new IllegalArgumentException("Unknown URI " + uri); } ContentValues value ; if(null != values){ value = new ContentValues(values) ; }else{ value = new ContentValues() ; } SQLiteDatabase db = mOpenHelper.getWritableDatabase() ; long rowId = db.insert( "main", null, values) ; // If the insert succeeded, the row ID exists. if (rowId > 0) { // Creates a URI with the note ID pattern and the new row ID appended to it. Uri noteUri = ContentUris.withAppendedId(Uri.parse(Main.CONTENT_URI + "/main/"), rowId); // Notifies observers registered against this provider that the data changed. getContext().getContentResolver().notifyChange(noteUri, null); return noteUri; } // If the insert didn't succeed, then the rowID is <= 0. Throws an exception. throw new SQLException("Failed to insert row into " + uri); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder sqb = new SQLiteQueryBuilder() ; sqb.setTables("main") ; switch (sUriMatcher.match(uri)) { case MAINS : sqb.setProjectionMap(columnMap) ; break ; case MAIN_ID : sqb.setProjectionMap(columnMap) ; sqb.appendWhere("_ID = " + uri.getPathSegments().get(1)) ; break ; } String orderBy; // If no sort order is specified, uses the default if (TextUtils.isEmpty(sortOrder)) { orderBy = "_ID"; } else { // otherwise, uses the incoming sort order orderBy = sortOrder; } SQLiteDatabase db = mOpenHelper.getReadableDatabase(); Cursor c = sqb.query( db, projection, selection, selectionArgs, null, null, orderBy) ; c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } // A string that defines the SQL statement for creating a table private static final String SQL_CREATE_MAIN = "CREATE TABLE " + "main " + // Table's name "(" + // The columns in the table " _ID INTEGER PRIMARY KEY, " + " WORD TEXT" + " FREQUENCY INTEGER " + " LOCALE TEXT )"; /** * Helper class that actually creates and manages the provider's underlying data repository. */ protected static final class MainDatabaseHelper extends SQLiteOpenHelper { /* * Instantiates an open helper for the provider's SQLite data repository * Do not do database creation and upgrade here. */ MainDatabaseHelper(Context context) { super(context, DBNAME, null, 1); } /* * Creates the data repository. This is called when the provider attempts to open the * repository and SQLite reports that it doesn't exist. */ public void onCreate(SQLiteDatabase db) { // Creates the main table db.execSQL(SQL_CREATE_MAIN); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } }
2.定义权限
在manifest 中定义,permission
在<manifest>节点里面,定义permission
<permission android:name="com.hualu.provider.WRITE"></permission> <permission android:name="com.hualu.provider.READ"></permission>
3.provider添加权限
在<provider> 节点里面添加
android:writePermission和android:readPermission
<provider android:authorities="@string/authority" android:name="ExampleContentProvider" android:writePermission="com.hualu.provider.WRITE" android:readPermission="com.hualu.provider.READ"></provider>
在另一个应用访问这个contentprovider
1.新建一个应用
2.在当前应用的manifest里面添加对之前定义的provider的权限的使用
<uses-permission android:name="com.hualu.provider.WRITE"/> <uses-permission android:name="com.hualu.provider.READ"/>3.在Activity里面通过ContentResolver调用provider
ContentValues values = new ContentValues() ; values.put("WORD", "abcd") ; Uri uri = this.getContentResolver().insert( Uri.parse("content://com.hualu.contentprovider/mains"), values) ; String id = uri.getPathSegments().get(1) ; Cursor cAll = this.getContentResolver().query( Uri.parse("content://com.hualu.contentprovider/mains"), null, null, null, null); Cursor c = this.getContentResolver().query( Uri.parse("content://com.hualu.contentprovider/main/1"), null, null, null, null); Toast.makeText(this, "insert success id = " + id + " ," + " \r\n All = " + cAll.getCount() + " , " + "\r\n one = " + c.getCount(), Toast.LENGTH_SHORT).show() ;
代码下载地址:
Contentprovider应用: http://download.csdn.net/detail/luhuajcdd/5140008
调用ContentProvider应用:http://download.csdn.net/detail/luhuajcdd/5140027