【发布时间】:2015-05-10 05:09:51
【问题描述】:
AppWidget 提供者(BroadcastReciever)
(尝试从 Stackview Widget 发送可序列化对象) 当我通过pendingIntent发送int作为额外时我能够在活动中接收但是当我尝试发送可序列化的额外时它给了我空指针异常 提前致谢。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_profile_stackview);
// set intent for widget service that will create the views
Intent serviceIntent = new Intent(context,WidgetProfieService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter( R.id.widget_profilestackimageview, serviceIntent);
Intent viewIntent = new Intent(context, ProfileActivity.class);
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
viewIntent.setData(Uri.parse(viewIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
remoteViews.setPendingIntentTemplate(R.id.widget_profilestackimageview, viewPendingIntent);
// update widget
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
RemoteViewService(发送额外的int(图像))
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_profile);
rv.setImageViewResource(R.id.stack_profile_pic, ((Profile) mWidgetProfiles.get(position)).getImageId());
Log.d("profile",mWidgetProfiles.get(position).getCountry());
Log.d("profile",mWidgetProfiles.get(position).getImageId()+"");
Bundle extras = new Bundle();
// extras.putInt("profile",mWidgetProfiles.get(position).getImageId());
//使用int额外的工作
extras.putSerializable("profile",(Profile)mWidgetProfiles.get(position));
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.profile_widgetlayout, fillInIntent);
try {
System.out.println("Loading view " + position);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return rv;
}
接收活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
/* 在这里使用 ExtraInt 有效 ImageView img=(ImageView) findViewById(R.id.activity_profilepic);
if(getIntent().getIntExtra("profile",0)!=0){ Log.d("widgetpic",getIntent().getIntExtra("profile",0)+""); img.setImageResource(getIntent().getIntExtra("profile",0)); }*/
ImageView img=(ImageView) findViewById(R.id.activity_profilepic);
Bundle extras = getIntent().getExtras();
if(extras !=null)
if(extras.containsKey("profile"))
{
Profile mProfile= (Profile)extras.getSerializable("profile");
img.setImageResource(mProfile.getImageId());
}
}
【问题讨论】:
-
还是一样!!
标签: android android-intent android-activity android-widget android-pendingintent