简体   繁体   中英

receiving result from intent service inside custom adapter for listview

what i am trying to do is the following

  1. I have a button inside my custom adapter layout that interact with php script on server to download a file on server
  2. the download procedure is handled by calling an IntentService in the background
  3. when the service is done i want to update my listview that is initiated inside an activity
  4. i am stuck with the Receiver class

my code so far is as follow :

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        if(IsRepository){
            view = vi.inflate(R.layout.filerepositorylayout, null);
        }
     }
      BindLayoutElemnts(view, position);
    return view;
  }

Here is where The Intent service is being called :

 private void BindLayoutElemnts(View view, int position) {
      myFiles = (files) getItem(position);
       img_Download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getContext(),DownloadService.class);
            intent.putExtra("FileID",""+myFiles.getID());
            intent.putExtra("FileName",myFiles.getName());
            context.startService(intent);
        }
    });
 }

The calling activity

Here is where the Receiver is being called

    public class user_repositry extends AppCompatActivity implements  DownloadReceiver.Receiver {
 protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       myDownloadReceiver = new DownloadReceiver(new Handler());
       myDownloadReceiver.setReceiver(this);
 }

  @Override
public void onReceiveResult(int resultCode, Bundle resultData) {
    Toast.makeText(user_repositry.this, "Downloaded", Toast.LENGTH_SHORT).show();

}

The Intent service code :

 ResultReceiver rec;
 @Override
protected void onHandleIntent(Intent intent) {
      rec = intent.getParcelableExtra("DownloadReceiver");
}

 private void UpdateDBRecord() {
    HashMap<String, String>  PostData=new HashMap<String, String>();
    PostData.put("call","UpdateFile");
    PostData.put("ID", ""+file_ID);
    BackgroundWorker registerWorker= new BackgroundWorker(this,this,PostData);
    registerWorker.setShowLoadingMessage(false);
    registerWorker.execute(Helper.getPhpHelperUrl());
}

this function is the function called in Post execute from the registerWorker above

 @Override
public void processFinish(String results) {
    Log.d("Download","Download DB updated");
    Bundle bundle = new Bundle();
    //bundle.putString("resultValue", "My Result Value. Passed in: " );
    // Here we call send passing a resultCode and the bundle of extras
    rec.send(Activity.RESULT_OK, bundle);
}

I am stuck with this error now :

Failed resolution of: Lcom/bassem/donateme/classes/DownloadReceiver;

I understood that the receiver is not being initiated but i can't seem to find the missing code in my logic

Appreciate your help

The missing code is not in your logic, it's in the logic of the DownloadReciever. Look for errors more broadly and figure out why that didn't load. That or publish some code from it so that it could be answered. Is it for example, looking for a uri that doesn't exist and thus crashes? What is it expecting and failing and why is it failing. You did not post any code that is crashing. You posted code that calls something that crashes.

Try this,

Step 1: Create the result handler using ResultReceiver inside activity

public ResultReceiver downloadReceiver = new ResultReceiver(new Handler()) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        //Broadcast /send the result code in intent service based on your logic(success/error) handle with switch
        switch (resultCode) {
            case 1: {
                //Get the resultData and do your logic here
                //Update your list view item here
                break;
            }
            case 2: {
                //Get the resultData and do your logic here
                //Update your list view item here
                break;
            }
        }
    }
};  

Step 2: Put the result handler inside the intent and start the intent service

Intent intent = new Intent(getContext(), DownloadService.class);
intent.putExtra("receiver",downloadReceiver);

Step 3: Handle the intent in service class(get the value of result handler)

final ResultReceiver receiver;

@Override
protected void onHandleIntent(Intent intent) {
    receiver = intent.getParcelableExtra("receiver");
}

Step 4: Send/Broadcast the data to the result handler

//based on your logic, change the result code and data

//Add your result data (success scenario)
Bundle bundle = new Bundle();
bundle.putString("result","Some text");
receiver.send(1,bundle);

//Add your result data (failure scenario)
receiver.send(0,bundle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM