2017-01-31 30 views
0

我已经实现了一个使用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 
    } 
+0

究竟做“它似乎没有这样的工作方式。”意思?另外我注意到你正在为一个Activity获得一个PendingIntent,但是你正在传递和Intent不是一个Activity。 –

回答

0

首先,您需要使用Activity从通知中启动。该活动将包含您想要显示的片段。在你的片段中有一个静态初始化器,它将采用你想传递给片段的必要参数。

/** 
    * 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); 
     } 
    } 

希望这是明确的

0

我认为你应该在未决意图中使用一个Activity,并且这个活动应该让你的frame_container在你的活动中创建,你可以从那里检查数据并设置片段,因为这样片段与你的通知动作无关。

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); 
相关问题