0

FragrmentクラスにBroadcasterクラスを作成したのは簡単です。私のパフォーマンスのためにAsystaskクラスを呼び出します。クラスを (onPostExecute メソッドで) 実行した後、Broadcaster を意図的に Fragment クラスに 1 回だけ送信します。

しかし、結果は OnReiver メソッドに 2 回入りました。(onPostExecuteメソッドに入り、ブロードキャスターを1回送信したことをLogを使用して確認しましたが、OnReiverメソッドは2回トリガーされました)。誰でも私を助けてくれます。それは私を悩ませ、多くの時間を無駄にしました。ありがとうございました

public class Fragment extends Fragment{ 

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      currentView = inflater.inflate(R.layout.left_panel, container, false);
      mContext = currentView.getContext();
      init();
      return currentView;
  }

  @Override
  public void onResume(){
      super.onResume();
      LocalBroadcastManager.getInstance(getActivity()).registerReceiver(albumListBroadcaster, new IntentFilter(CommonUtilities.BROADCASTEROFALBUMTITLELIST));
  }

  private void init(){

      albumListBroadcaster = new AlbumListBroadcaster();

      setOnClickButtonsEvent();
      clearBtn();
  }

  public void setOnClickButtonsEvent(){
      //modifyBtn.setOnClickListener(new ModifyAccountBtnListener());
      modifyBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, WebActivity.class);
                startActivityForResult(intent, REQUESTCODE);
            }
        });
  }


   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intentData) {

     // TODO Auto-generated method stub
        if(requestCode == REQUESTCODE){
            if(resultCode == RESULT_OK){
                //sendBroadcaster();
                data = this.getActivity().getSharedPreferences(CommonUtilities.SHAREDPREFERENCEURL, 0);
                String usertToken = data.getString("userToken", "");
                String mobileUserId = data.getString("mobileUserId", "");
                String services = intentData.getStringExtra("servicesJSONArray");

                try {
                    JSONArray socialMedias = new JSONArray(services);
                    Log.d("socialMedias", socialMedias.toString());
                    clearBtn();
                    //JSONArray accountInfo = new JSONArray(services);
                    mDisplayPicOnAlbumFragment.onDisplayPicOnAlbumFragment(new JSONArray());
                    if(socialMedias.length() > 0){

                        totalArrayForAlbum = new JSONArray();
                        int lastNum = socialMedias.length();

                        for(int i = 0;i< lastNum;i++){
                            JSONObject one = socialMedias.getJSONObject(i);
                            String socialName = one.getString("service_display_name");
                            activateBtn(i,socialName);
                            String serviceName = one.getString("service_name");
                            Map <String,String> map = new HashMap<String,String>();
                            map.put("userToken", usertToken);
                            map.put("userId", mobileUserId);
                            map.put("serviceName", serviceName);
                            new RequestAlbums(map,getActivity()).execute(CommonUtilities.ALBUMLISTURL);

                        }
                    }else{
                        ArrayList<String> emptyArr = new ArrayList<String>();
                        emptyArr.add(getString(R.string.emptyList));
                        createMenu(emptyArr);

                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
     super.onActivityResult(requestCode, resultCode, intentData);
}


public class AlbumListBroadcaster extends BroadcastReceiver{

    String title = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("OnRecive","i am here");

       } 
}


   public class RequestAlbums extends AsyncTask<Object,Object,String>{

private HttpEntity httpEntity;
private HttpGet httpGet;
private HttpClient httpClient;
private InputStream inputStream;
private HttpResponse httpResponse;
private Map<String,String> param;
private Context mContext;

public RequestAlbums(Map<String,String> param,Context c){
    this.param = param;
    mContext = c;
}

@Override
protected void onPreExecute(){
    // TODO Auto-generated method stub
    super.onPreExecute();
}

 @Override
 protected void onPostExecute(String result) {
        // TODO Auto-gworkenerated method stub
      Log.d("background", result);
      ArrayList<String> resultArr = getOnlyTitle(result);
      Intent intent = new Intent(CommonUtilities.BROADCASTEROFALBUMTITLELIST);
      intent.putStringArrayListExtra("titleList", resultArr);
      intent.putExtra("resultInfo", result);
      //intent.putExtra("titleList", resultArr.toString());

      LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
      //super.onPostExecute(result);
}

@Override
protected String doInBackground(Object... input) {

    ---
}


  } 
4

1 に答える 1