Android: How to run asynctask from different class file?

后端 未结 4 2017
醉酒成梦
醉酒成梦 2020-12-01 02:18

When I use my code in one class file, it runs perfectly:

package com.example.downloadfile;

import java.io.File;
import java.io.FileOutputStream;
import java         


        
4条回答
  •  感动是毒
    2020-12-01 02:43

    If you can somehow pass the Activity class or its context to the AsyncTask that will solve your issue for showing dialog. You would need to include another parameter together with the URL you are sending and put that parameter in a Context variable. And then whenever you need the dialog you use that context variable to show it.

    If the dialog does not have a Context from which to show it will definitely run into runtime errors.

    Update (put my comment up here as well): here we go... found a good example that you can modify to make use for your case. It's at brighthub.com/mobile/google-android/articles/82805.aspx. Scroll down to Source Code section and have a look at the code for WebServiceAsyncTask and WebServiceBackgroundActivity.

提交回复
热议问题