内容提供商瓦特/光标 - 就会被杀死(如何通知客户端) [英] Content Provider w/ Cursor - Gets Killed (how to notify client)

查看:158
本文介绍了内容提供商瓦特/光标 - 就会被杀死(如何通知客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内容提供商在APK是提供基于REST的搜索结果为多个客户端应用程序。

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 an intent service request to trigger the search..

在提供商,光标返回到一个表,并且每个意图的服务请求填充相应的表,并触发NotifyDataSetChanged。

In the provider, the cursor is returned to a table, and each intent service request populates the corresponding table, and triggers a NotifyDataSetChanged.

作品真的很好..我遇到的唯一问题是,如果供应商被杀害由于某种原因光标上的客户端APK不会改变(做pkill的-9 com.myprovider.name测试),但应用程序不通知供应商走了,就需要重新连接到数据库..所以它会继续火过服务的意图,光标永远不会被更新b / C它不再依赖于底层的数据库。

Works really well.. The only issue I am having is that if the Provider is killed for some reason (tested by doing pkill -9 com.myprovider.name) the cursor on the client apk does not change, but the app is not notified that the provider went away, and would need to re-connected to the db.. So it will continue to fire off service intents, and the cursor never gets updated b/c it is no longer tied to the underlying db..

我通过我们的code去试试,看看我们捉一些例外,也许隐藏了底层的问题,但我没有看到它。

I went through our code to try and see if we were catching some exceptions, that maybe hiding the underlying issue, but I am not seeing it..

我试着明确做cursor.close()在敲定/ onLowMemory /关机()的供应商..没有出现被触发。

I tried explicitly doing a cursor.close() in the provider in finalize / onLowMemory / shutdown().. None appear to get triggered.

我注意到,这发生在logcat中

I noticed that this happens in the logcat

09-10 13:58:03.015: I/ActivityThread(4268): Removing dead content provider: com.myprovider.name (called from the calling app)

什么办法让从应用程序调用此通知?

Any way to get this notification from the calling app?

推荐答案

于是经过一番研究,我有一个可行的解决方案,但会如果任何人有任何其他建议,他们将AP preciated。

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

由于这样的事实,我们使用的是ContentProvider的得到一个光标,然后通过刚更新IntentService后端数据。我们抓住光标当我们的用户在他们的搜索的第一个字符,如果光标计数= 0,那么我们将展示一个空列表的消息,否则我们在光标显示的项目清单。

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..

**注意:有些光标操作可能需要进行调整了一下,我没有看到任何崩溃瓦特/有一个光标拆出来,成为空,但您的里程可能会改变我们适配器。(这是更有关使用contentProviderClient())

** 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() )

**注意:每文档,你必须释放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

private ContentProviderClient contentProviderClient = null;

然后,当我们的用户改变了我们最终调用搜索字符串

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

public void setFilter( 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 exit

   if (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 );
}

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

这篇关于内容提供商瓦特/光标 - 就会被杀死(如何通知客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆