我已经实现了一个使用AsyncTask的通知,我想要做的是通知单击时传递数据。每当我点击通知时,我应该继续处理一个将传递数据的片段。我正在使用Bundle
将数据传递给我的片段类。 如何捆绑数据传递给片段onClick通知将数据传递到片段使用包
我只使用意图和去除捆绑尝试,但它不会做任何事情
new notifyThis(this, "2", "Title", "Description", "http://imgur.com/gallery/WBTdB").execute();
通知的AsyncTask
public class notifyThis extends AsyncTask<String, Void, Bitmap> {
private Context mContext;
private String itemId, title, description, imageLink;
Notification notification;
NotificationManager notificationManager;
DetailsFragment detailsFragment;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Bundle bundle;
PendingIntent pendingIntent;
Intent intent;
Resources res;
public notifyThis(Context context, String itemId, String title, String description, String imageLink) {
super();
this.mContext = context;
this.itemId = itemId;
this.title = title;
this.description = description;
this.imageLink = imageLink;
}
@Override
protected Bitmap doInBackground(String... params) {
InputStream in;
try {
URL url = new URL(this.imageLink);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
in = connection.getInputStream();
return BitmapFactory.decodeStream(in);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap imageResult) {
super.onPostExecute(imageResult);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
final int idItem= Integer.valueOf(itemId);
intent = new Intent(mContext, DetailsFragment.class);
bundle = new Bundle();
bundle.putInt("id", idItem);
detailsFragment= new DetailsFragment();
detailsFragment.setTitle(title);
detailsFragment.setArguments(bundle);
transaction.replace(R.id.frame_container, detailsFragment);
transaction.addToBackStack("details");
transaction.commit();
intent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_ONE_SHOT);
res = mContext.getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
imageResult = Bitmap.createScaledBitmap(result, width, height, false);
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(mContext)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(imageResult)
.setSubText(description)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}
}
这是怎么了我得到的数据通过 - DetailsFragment.class
if (save != null) {
setTitle(save.getString("title"));
}
if (itemId != this.getArguments().getInt("id")) {
itemId = this.getArguments().getInt("id");
/do something here
}
究竟做“它似乎没有这样的工作方式。”意思?另外我注意到你正在为一个Activity获得一个PendingIntent,但是你正在传递和Intent不是一个Activity。 –