Android:运行在单独进程中的ContentProvider


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

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

  1. public IBulkCursor bulkQuery(Uri url, String[] projection,  
  2.         String selection, String[] selectionArgs, String sortOrder, IContentObserver observer,  
  3.         CursorWindow window) throws RemoteException;  
  4. public Cursor query(Uri url, String[] projection, String selection,  
  5.         String[] selectionArgs, String sortOrder) throws RemoteException;  
  6. public String getType(Uri url) throws RemoteException;  
  7. public Uri insert(Uri url, ContentValues initialValues)  
  8.         throws RemoteException;  
  9. public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException;  
  10. public int delete(Uri url, String selection, String[] selectionArgs)  
  11.         throws RemoteException;  
  12. public int update(Uri url, ContentValues values, String selection,  
  13.         String[] selectionArgs) throws RemoteException;  
  14. public ParcelFileDescriptor openFile(Uri url, String mode)  
  15.         throws RemoteException, FileNotFoundException;  
  16. public AssetFileDescriptor openAssetFile(Uri url, String mode)  
  17.         throws RemoteException, FileNotFoundException;  
  18. public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)  
  19.         throws RemoteException, OperationApplicationException;  

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

  1. public abstract boolean onCreate();  
  2. public void onConfigurationChanged(Configuration newConfig);  
  3. public void onLowMemory();  

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

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

相关内容