运行在单独进程中的ContentProvider

本文详细解析了Android中ContentProvider的工作原理,特别是当它运行在独立进程中时如何通过IPC进行跨进程通信。此外还强调了某些方法不在IPC范围内,应该避免在特定方法中执行预期在ContentProvider进程中运行的任务。

http://blog.youkuaiyun.com/windskier/article/details/6682419


ContentProvider既可以与调用方处在同一进程,也可以运行在单独进程中,完全取决于ContentProvider所处的aplication的进程信息。因此假如ContentProvider运行在单独的进程中,那么调用ContentProvider将会涉及到IPC通信。

既然涉及到IPC通信,那么ContentProvider一定继承自IInterface,这个IInterface就是IContentProvider,其主要的接口方法如下,

public IBulkCursor bulkQuery(Uri url, String[] projection,  
        String selection, String[] selectionArgs, String sortOrder, IContentObserver observer,  
        CursorWindow window) throws RemoteException;  
public Cursor query(Uri url, String[] projection, String selection,  
        String[] selectionArgs, String sortOrder) throws RemoteException;  
public String getType(Uri url) throws RemoteException;  
public Uri insert(Uri url, ContentValues initialValues)  
        throws RemoteException;  
public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException;  
public int delete(Uri url, String selection, String[] selectionArgs)  
        throws RemoteException;  
public int update(Uri url, ContentValues values, String selection,  
        String[] selectionArgs) throws RemoteException;  
public ParcelFileDescriptor openFile(Uri url, String mode)  
        throws RemoteException, FileNotFoundException;  
public AssetFileDescriptor openAssetFile(Uri url, String mode)  
        throws RemoteException, FileNotFoundException;  
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)  
        throws RemoteException, OperationApplicationException;  


但是阅读ContentProvider代码时,会发现

public abstract boolean onCreate();  
public void onConfigurationChanged(Configuration newConfig);  
public void onLowMemory();


这几个方法并不在IContentProvider中声明,因此千万注意它们在运行时并不是处在AndroidManifest.xml中声明时的进程中。而是处在调用ContentProvider的应用的进程中的。

因此千万不要在onCreate中添加你期望运行在ContentProvider的进程的代码。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值