我开发了一个备忘应用程序。我用清单显示笔记。如果用户愿意,他可以在主屏幕上添加带有注释内容的小部件。我提供了这些内容,但是如果用户更新了他添加的小部件的注释,则必须更新。实际上,此事件(发生更新)一直在调试模式下发生。但是我建立并运行普通模式。此事件有时是,但有时不是。请帮帮我。
public class MyNotesWidget extends AppWidgetProvider {
static MySharedPref mySharedPref;
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
if (0 == MyNotesWidgetActivity.widgetProcess.compareTo("create")) {
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
mySharedPref.saveNoteWidget(context, CustomAdapter.cid, appWidgetId);
} else { // update section
appWidgetManager.updateAppWidget(appWidgetId, null); // ı set null value to view
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public static RemoteViews updateWidgetListView(Context context, int appWidgetId) {
if (0 == MyNotesWidgetActivity.widgetProcess.compareTo("create")){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_notes_widget);
Intent svcIntent = new Intent(context, WidgetViewService.class);
svcIntent.setData(Uri.fromParts("content", String.valueOf(CustomAdapter.cid), null));
remoteViews.setRemoteAdapter(R.id.listViewWidget, svcIntent);
return remoteViews;
}else{ // ı use static values. CustomAdapter.uid is meaning chosen note id.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_notes_widget);
Intent svcIntent = new Intent(context, WidgetViewService.class);
svcIntent.putExtra("widgetId",appWidgetId);
svcIntent.setData(Uri.fromParts("content", String.valueOf(CustomAdapter.uid), null));
remoteViews.setRemoteAdapter( R.id.listViewWidget, svcIntent);
return remoteViews;
}
}
}
public class WidgetViewService extends RemoteViewsService {
@Override
public RemoteViewsService.RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new WidgetListProvider(this.getApplicationContext(), intent));
}
}
public class WidgetListProvider implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private int appWidgetId;
private MySharedPref mySharedPref;
private int noteId;
private int widgetId;
private int size = 1;
private AppWidgetManager appWidgetManager;
public WidgetListProvider(Context context, Intent intent) {
this.appWidgetManager = AppWidgetManager.getInstance(context);
this.context = context;
this.noteId = Integer.valueOf(intent.getData().getSchemeSpecificPart());
if(0 != MyNotesWidgetActivity.widgetProcess.compareTo("create")){
this.widgetId = intent.getIntExtra("widgetId",0);
}
}
@Override
public int getCount() {
return size;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getViewAt(int position) {
String content;
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
DataSource db = new DataSource(context);
db.open();
content = db.getNote(noteId);
remoteView.setTextViewText(R.id.content, content);
if(0 != MyNotesWidgetActivity.widgetProcess.compareTo("create")){
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.content);
}
return remoteView;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
}
@Override
public void onDestroy() {
}
}