如何观察内容提供者的变化?安卓

时间:2012-06-28 04:11:33

标签: android android-contentprovider android-appwidget notify

我创建了一个Contentprovide并实现了它的update()方法,如下所示:

@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中收到通知?谢谢!

我正在尝试使用此解析器但没有日志(-------------------- 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个步骤解决了它:

  1. 添加 this.getContext().getContentResolver().notifyChange(URI, null);
  2. 到ContentProvider的updat()insert()delete()回调。

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

    2. 在Observer的onChange()中调用notifyAppWidgetViewDataChanged()。

    3. 删除小部件并放置一个新小部件。

    4. 就是这样。

1 个答案:

答案 0 :(得分:17)

您可以使用ContentObserver课程。您只需实现该类并使用getContentResolver().registerContentObserver()方法注册观察者。