我已經實現了一個使用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。 –