Skip to content Skip to sidebar Skip to footer

Content Provider W/ Cursor - Gets Killed (how To Notify Client)

I have a content provider in on apk which is providing Rest Based Search results for multiple client apps. Basically, the client requests a cursor via content resolver, then fires

Solution 1:

So after some research, I have a workable solution, but would if anyone has any other suggestions, they would be appreciated.

Due to the fact we are using a contentProvider to get a cursor, then just updating the back end data via IntentService. We grab the cursor when our user types the first character in of their search, if the cursor count = 0, then we show an empty list message, otherwise we display the list of items in that cursor..

** NOTE: Some of the cursor handling may need to be tweaked a bit, I did not see any crashes w/ our Adapter having a cursor ripped out and becoming null, but your mileage may vary.. ( This is more about using the contentProviderClient() )

** NOTE: Per the documentation, you MUST release the contentProviderClient() when not in use anymore.

Since a content provider should be returning a type, we can do the following..

So we define a member variable

privateContentProviderClientcontentProviderClient=null;

Then when our user changes the search string we end up calling

publicvoidsetFilter(String searchFilter ) {
   searchedString = !TextUtils.isEmpty(filter) ? filter : "";

   boolean reload = false;               // Reloads content provider// contentProvider is null on first connection, assumed to be up on // subsequent connections as we only close the cursor and // contentProviderClient when we exitif (contentProviderClient != null) {
      try {
      // getType will throw an exception if the instance of the // contentProvider went away (killed by user/memory collection etc)

         contentProviderClient.getType(searchFactoryUri);
      } catch (RemoteException e) {
         if( searchAdapter != null ) {
            Cursor cursor = searchAdapter.getCursor();
            cursor = null; 
            reload = true;
            contentProviderClient.release();
         }
      }
   }

   // This is for first search initialization or reloading cursor // if the content provider went away for some unknown reason.if( this.searchAdapter == null || reload ){
      Cursor cursor = getActivity().getContentResolver().query(
            MY_URI,
            null,
            null,
            null,
            null);

   contentProviderClient = getActivity()
             .getContentResolver()
             .acquireContentProviderClient(MY_URI);

   cursor.setNotificationUri(getActivity.getContentResolver(), MY_URI);

   // DO what ever you need to after getting cursor, a cursor loader// would be a better implementation here, but simplifying it as // I don't want to over-complicate the example.

   myList.setAdapter(searchAdapter);

   }
   getActivity().startService(MyUtil.getSearchIntent( MY_URI, searchedString );
}

@OverridepublicvoidonDestroy() {
   super.onDestroy();
   // Cleanup adapter, cursor, provider clientif (searchAdapter != null) {
      searchAdapter.changeCursor(null); // Closes existing cursor 
   }
   if (contentProviderClient != null) {
     contentProviderClient.release();  // Release client
   }
}

Post a Comment for "Content Provider W/ Cursor - Gets Killed (how To Notify Client)"