我已经使用AsyncTask实现了通知,我想要做的是通知单击时传递数据。每当我点击通知时,我都应该进入一个片段,该片段将传递数据。我正在使用Bundle
将数据传递给我的Fragment类。
如何将包数据传递给Fragment
我尝试仅使用Intent并删除Bundle但它没有做任何事情
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
}
答案 0 :(得分:0)
我认为您应该在待处理意图中使用一个活动,并且此活动应该在您的活动中包含您的frame_container,您可以检查数据并从那里设置片段,因为这样片段与您的通知操作无关。
答案 1 :(得分:0)
将数据添加到分发包并创建待处理的意图。将此添加到您的通知管理器
Intent intent = new Intent(getActivity(), MainActivity.class);
Bundle bundle=new Bundle();
intent.putExtra(BUNDLE_KEY,bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_icon)
.setContentTitle(title)
.setContentText(result)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
答案 2 :(得分:-1)
首先,您需要使用“活动”从通知中启动。活动将容纳您要显示的片段。在片段中有一个静态初始值设定项,它将获取您想要传递给片段的必要参数。
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
* @return
*/
public static ProductFragment newInstance(String category) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putString(ARG_CATEGORY, category);
fragment.setArguments(args);
return fragment;
}
在您的活动中添加从此方法返回的片段实例(使用片段管理器添加标准片段)。您可以像{ - 1}}这样从片段中获取参数 -
onCreate
希望这很清楚