如何观察ContentProvider的变化

如何观察ContentProvider的变化

本文介绍了如何观察ContentProvider的变化?机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我心中已经建立了一个Contentprovide并实现它的update()方法是这样的:

I'v created a Contentprovide and implements it's update() method like this:

@Override
   public int update(Uri uri, ContentValues values, String selection,
         String[] selectionArgs) {
      final SQLiteDatabase db = mHelper.getWritableDatabase();
      int count = db.update(InslideDB.FOLDER_TABLE, values, selection, selectionArgs);

      /* To notify the observer */
      this.getContext().getContentResolver().notifyChange(URI, null);
      return count;
}

我怎样才能在我的appwidget通知?谢谢!

How can I get notified in my appwidget? Thanks!

我想这个解析器,但没有登录(--------------------更改日期)打印:

I'm trying this Resolver but no Log (--------------------Date Changed) print:

class DataProviderObserver extends ContentObserver {
    private AppWidgetManager mAppWidgetManager;
    private ComponentName mComponentName;

    DataProviderObserver(AppWidgetManager mgr, ComponentName cn, Handler h) {
        super(h);
        mAppWidgetManager = mgr;
        mComponentName = cn;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);  //NOTE: Have to call this. dwy.

        // The data has changed, so notify the widget that the collection view needs to be updated.
        // In response, the factory's onDataSetChanged() will be called which will requery the
        // cursor for the new data.

        Log.d("PresenterWidgetProvider", "--------------------Date Changed");

        mAppWidgetManager.notifyAppWidgetViewDataChanged(
                mAppWidgetManager.getAppWidgetIds(mComponentName), R.id.inslide_grid);
    }
}


public class PresenterWidgetProvider extends AppWidgetProvider{

    private static final String AUTHORITY = "com.apps.slide";

    static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/folder");

    public static final String EXTRA_INSLIDE_PATH = "EXTRA_WIDGET_PATH";
    private static HandlerThread sWorkerThread;
    private static Handler sWorkerQueue;
    private static DataProviderObserver sDataObserver;

    public PresenterWidgetProvider () {

        // Start the worker thread to update Content Provider.
        sWorkerThread = new HandlerThread("PresenterWidgetProvider worker");
        sWorkerThread.start();
        sWorkerQueue = new Handler(sWorkerThread.getLooper());

    }

    @Override
    public void onEnabled(Context context) {
        // Register for external updates to the data to trigger an update of the widget.  When using
        // content providers, the data is often updated via a background service, or in response to
        // user interaction in the main app.  To ensure that the widget always reflects the current
        // state of the data, we must listen for changes and update ourselves accordingly.
        final ContentResolver r = context.getContentResolver();
        if (sDataObserver == null) {
            final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
            final ComponentName cn = new ComponentName(context, PresenterWidgetProvider.class);
            sDataObserver = new DataProviderObserver(mgr, cn, sWorkerQueue);
            r.registerContentObserver(CONTENT_URI, true, sDataObserver);
            Log.d("PresenterWidgetProvider", "--------------------onEnabled. sDataObserver = " + sDataObserver);
        }
    }

所以,我终于通过4个步骤解决了:

So, I finally solved it by 4 steps:

  1. 添加 this.getContext()getContentResolver()有NotifyChange(URI,空);
  1. Addthis.getContext().getContentResolver().notifyChange(URI, null);

要的ContentProvider的updat()插入()删除()回调。

to ContentProvider's updat() insert() delete() callbacks.

  1. 创建一个新的观察员和新的线程来处理它。

  1. Create a new Observer and new Thread to handle it.

在观察的的onChange()调用notifyAppWidgetViewDataChanged()。

Call notifyAppWidgetViewDataChanged() in Observer's onChange().

删除小部件,并把一个新的。

Delete the widget and place a new one.

就是这样。

推荐答案

您可以使用<$c$c>ContentObserver类。你只需要实现该类并使用 getContentResolver()。registerContentObserver()方法来注册的观察者。

You can use the ContentObserver class. You just have to implement that class and use the getContentResolver().registerContentObserver() method to register the observer.

这篇关于如何观察ContentProvider的变化?机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:01