简体   繁体   English

如何将每个AsyncTask类放在单独的文件中?

[英]How do put each AsyncTask class in a separate file?

I have a few asynstask that are definined inside of my main activity. 我在主要活动中定义了一些asynstask。 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. 顺便说一句,如果我将这段代码原样(不带导入)放置在mainActivity中,它就可以正常工作。 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. 为此,您需要了解AsyncTask具有哪些依赖项。

To fire Intents you need Context intance. 要触发意图,您需要Context实例。 I also see some twitter variable. 我还看到一些twitter变量。

So you need declare appropriate fields and to pass those objects to your TwitterLogin constructor. 因此,您需要声明适当的字段并将这些对象传递给您的TwitterLogin构造函数。

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: 稍后您可以触发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. 重要的是要理解,所有这些方法(例如startActivity)都不是某些“全局函数”,而是某些类实例的方法,您不能仅从AsycTask实例中调用这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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