0
//global
private AsyncTask<String,String,String> myAsync;

//inside my activity
public class Myreceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String arg[] = {intent.getStringExtra("id")};
        myAsync= new AsyncTask<String, String, String>(); //error here
        myAsync.execute(arg);
    }
}

错误是:

无法实例化类型 AsyncTask

为什么 ?

4

1 回答 1

1

它是一个示例 AsyncTask Activity..

public class StockWatch extends Activity {


String myData;
Button refresh;
myHtmlParser myParser;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    refresh = (Button)findViewById(R.id.refresh);
    myParser = new myHtmlParser();   
    refresh.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            myAsyncTask myWebFetch = new myAsyncTask();
            myWebFetch.execute();
        }
    });
}

class myAsyncTask extends AsyncTask<Void, Void, Void>    {

    TextView tv;

    myAsyncTask()    {
        //default constructor
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        //do your stuff
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //do your stuff
    }

    @Override
    protected Void doInBackground(Void... arg0) {
          //do your stuff
    }   
}   

}

有关更多详细信息,请参见此处

于 2013-10-15T13:11:40.637 回答