打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
揭秘Android Widget的bug之Widget与Activity之间的数据传递 - water的日志 - 网易博客

揭秘AndroidWidget的bug之Widget与Activity之间的数据传递

Android研究2010-07-30 15:45:19阅读7评论0  字号: 订阅

在我们创建Widget后,需要通过点击Widget再次进入Activity,这时候往往需要传递数据,我们可以通过设置PendingIntent的方式在Intent中存放数据达到目的。但是Intent的putExtra()方法无法携带数据,而只能用setData()方法传递,这在之前的外包项目中遇到过,今天在修改EasyNote时再次遇到,所以觉得有必要分享出来。具体代码如下:

 
public class NoteWidget extends AppWidgetProvider {

private String TAG = NoteWidget.class.getSimpleName();
private NotePadDbAdapter mAdapter;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
Log.i(TAG, "onUpdate id=" + appWidgetIds[i]);
int noteId = setting.getNoteWidgetId(appWidgetIds[i]);
if(noteId != -1){
Note note = mAdapter.fetchNoteById(noteId);
Log.i(TAG, "onUpdate noteId=" + noteId);
if (null != note) {
Log.i(TAG, "onUpdate note title=" + note.title);
updateWidget(appWidgetManager,context, appWidgetIds[i], note);
}
}
}

}

@Override
public void onReceive(Context context, Intent intent) {

}


private void updateWidget(AppWidgetManager appWidgetManager, Context context, int appWidgetId, Note note) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setTextViewText(R.id.note_body, note.body);
views.setTextColor(R.id.note_body, note.textColor);
views.setTextViewText(R.id.note_title, note.title);
views.setTextColor(R.id.note_title, note.textColor);
views.setImageViewResource(R.id.note_bg, MainActivity.s_note_bg[note.textBackground]);


Intent intent = new Intent(context,ViewNoteActivity.class);
'''//用setData()方法可以传递数据,但是用putExtra()却不可以达到传递数据的目的'''
Uri dataUri = Uri.parse(String.valueOf(note.id));
intent.setData(dataUri);
//intent.putExtra(ViewNoteActivity.EXTRA_NOTE_ID, note.id);
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
views.setOnClickPendingIntent(R.id.note_item, mPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}



@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}

}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android桌面小插件的开发详解
转载一份 关于appwidget 很详细
自己的AppWidget一个简单教程 - 开发 - Android - JavaEye群组
AppWidget的使用
Android开发中的窗口小部件App Widgets
AppWidget控件入门详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服