2017-01-31 63 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); 
相關問題