@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中获得通知?谢谢!
我正在尝试这个Resolver,但没有日志(——————–更改日期)打印:
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个步骤解决了:
>添加
this.getContext().getContentResolver().notifyChange(URI,null);
到ContentProvIDer的updat()insert()delete()回调.
>创建一个新的观察者和新的线程来处理它.
>在Observer的onChange()中调用notifyAppWidgetVIEwDataChanged().
>删除小部件并放置一个新的.
而已.
解决方法 您可以使用ContentObserver
类.您只需要实现该类,并使用getContentResolver().registerContentObserver()方法来注册观察者. 总结 以上是内存溢出为你收集整理的如何观察内容提供者的变化?安卓全部内容,希望文章能够帮你解决如何观察内容提供者的变化?安卓所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)