简体   繁体   中英

How do put each AsyncTask class in a separate file?

I have a few asynstask that are definined inside of my main activity. I tried to make the code more modular by putting each one of these classes on a separate file. Unfortunately I keep getting some errors such as not being able to get the intents to work. How do I connect this code with my main activity. By the way if I place this code as is(without the imports) in the mainActivity it works just fine. Thanks

package com.example.food4thought;

import java.net.URL;

import twitter4j.TwitterException;
import twitter4j.auth.RequestToken;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;



// Starts an intent that loads up a web browser and asks the user to log in to twitter
    // and get a pin#
    public  class TwitterLogin extends AsyncTask<URL, Integer, RequestToken>  {



        protected RequestToken doInBackground(URL... arg0) {

            try {
                requestToken = twitter.getOAuthRequestToken();
                Log.i("Got Request Token", "food4thought");
            } catch (TwitterException e) {
                Log.i("Failed to get Request Token", "food4thought");
            }

            //Log.i(requestToken.getAuthorizationURL(), "food4thought");
            //requestToken.getAuthorizationURL();
            //log_in.setText();

            try {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthorizationURL()));
            startActivity(browserIntent);
            }

            catch(NullPointerException e) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Unable to log in, No access to the Internet.", Toast.LENGTH_LONG).show();

                    }
                });

            }

            return null;
        }

    }

To do that you need to understand what dependencies your AsyncTask has.

To fire Intents you need Context intance. I also see some twitter variable.

So you need declare appropriate fields and to pass those objects to your TwitterLogin constructor.

Something like that:

public  class TwitterLogin extends AsyncTask<URL, Integer, RequestToken>  {
    private Context context;
    //other fields here

    public TwitterLogin(Context context, ...){ // other variables here
        this.context = context;
        //other fields assignment
    }
}

Later you can fire Intent:

context.startActivity(browserIntent);

What's important to understand is that all those methods like startActivity are not some "global functions", rather they are methods of some class instance, and you can't just call those methods from AsycTask instance.

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