Android onClick通知使用bundle将数据传递到片段
我已经使用AsyncTask实现了一个通知,我想做的是让通知在单击时传递数据。每当我点击通知时,我都会进入一个片段,该片段将获得传递的数据。我正在使用Android onClick通知使用bundle将数据传递到片段,android,android-fragments,Android,Android Fragments,我已经使用AsyncTask实现了一个通知,我想做的是让通知在单击时传递数据。每当我点击通知时,我都会进入一个片段,该片段将获得传递的数据。我正在使用Bundle作为要在片段类上传递的数据。 如何将捆绑包数据传递给片段 我试着只使用Intent并删除Bundle,但它没有任何作用 new notifyThis(this, "2", "Title", "Description", "http://imgur.com/gallery/WBTdB").execute(); 通知异步任务 public
Bundle
作为要在片段类上传递的数据。
如何将捆绑包数据传递给片段
我试着只使用Intent并删除Bundle,但它没有任何作用
new notifyThis(this, "2", "Title", "Description", "http://imgur.com/gallery/WBTdB").execute();
通知异步任务
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);
}
}
我认为您应该在挂起的意图中使用活动,并且此活动应该在创建时将您的框架容器放在您的活动中。您可以检查数据并从中设置片段,因为这样片段与您的通知操作没有任何关系。将数据添加到捆绑包并创建挂起的意图。将此添加到通知管理器
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);
首先,您需要使用活动从通知启动。该活动将包含要显示的片段。在片段中有一个静态初始值设定项,它将获取要传递给片段的必要参数
/**
* 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;
}
将此方法返回的片段实例添加到活动中(使用片段管理器添加标准片段)。您可以从onCreate
中的片段中获取参数,如下所示-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCategory = getArguments().getString(ARG_CATEGORY);
}
}
希望这是清楚的到底什么是“它似乎不这样工作”的意思?我还注意到你正在为一项活动准备一个悬挂式帐篷,但你的传球和意图不是一项活动。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCategory = getArguments().getString(ARG_CATEGORY);
}
}