如何将值传递给非活动类

时间:2013-09-09 19:55:40

标签: java android android-asynctask sharedpreferences

我想将活动中的两个值传递给AsyncTask类,并将它们从后台进程发送到SOAP Web服务,但它返回我的null或者错误,我确定在从LoginActivity传递值到AsyncTask时会有一些wrnog。< / p>

这是我的LoginActivity代码:

        final EditText LoginId = (EditText) findViewById(R.id.IDLogin);
    final EditText LoginPass = (EditText) findViewById(R.id.LoginPass);
    contextOfApplication = getApplicationContext();
    mPrefs = getSharedPreferences(PREFS, 0);
    boolean rememberMe = mPrefs.getBoolean("rememberMe", false);

    final String login1 = LoginId.getText().toString();
    final String pass1 = LoginPass.getText().toString();

    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(LoginActivity.this);
    prefs.edit().putString("login1", login1).commit();
    prefs.edit().putString("password1", pass1).commit();

这里是调用并将活动上下文传递给AsyncTask Constractor:

        loginBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            ProgressDialog progressDialog = new ProgressDialog(
                    LoginActivity.this);
            progressDialog.setMessage("جاري تسجيل الدخول الرجاء الانتظار");
            progressDialog.show();

            AsyncTaskWebServiceCaller MyTask = new AsyncTaskWebServiceCaller(
                    LoginActivity.this, progressDialog,
                    getApplicationContext());
            MyTask.execute();

        }
    });

我的完整AsyncTask代码:

 public class AsyncTaskWebServiceCaller extends AsyncTask<Void, Void, String> {

Activity mActivity;
Context context;


 LoginActivity MyClass = new LoginActivity();
 public static Context contextOfApplication;
ProgressDialog progressDialog;

 Context applicationContext = LoginActivity.getContextOfApplication();

// Constractor
public AsyncTaskWebServiceCaller(Activity activity,
        ProgressDialog progressDialog, Context context) {
    super();
    this.progressDialog = progressDialog;
    this.mActivity = activity;
    this.context = context;
}

// BackGround Process
@Override
protected String doInBackground(Void... voids) {
    // this is executed in a background thread.
    // the result is returned to the UI thread via onPostExecute

    try {
        final String NAMESPACE = "http://ws.sams.com";
        final String URL = "http://88.198.82.92:8080/sams1/services/LoginActvityWs?WSDL"; // usint
                                                                                            // //
                                                                                            // localhost
        final String METHOD_NAME = "login";
        final String SOAP_ACTION = "http://ws.sams.com/login";
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        final HttpTransportSE androidHttpTransport = new HttpTransportSE(
                URL);




        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
        String user = prefs.getString("login1", null);
        String pass = prefs.getString("password2", null);

2 个答案:

答案 0 :(得分:1)

将值传递给AsyncTask子类:

1-传递给他们一个构造函数:

class MyTask extends AsyncTask<Void,Void,Void>{
    MyObject myObject = null;
    public MyTask(MyObject myObject){
        this.myObject = myObject;
    }
//....

2-在execute()方法参数中传递它:

// your code on the Main thread which will call the execute() method
// ....
new MyTask().execute(myObject);    // i dont remember the exact name of this method, any way
来自AsyncTask子类的

和片段将是

class MyTask extends AsyncTask<Void,MyObject,Void>{

@Override
public void doInBackGround(MyObject...params){
    MyObject myObject = params[0];
    // the rest of your code
}

请记住,如果你想做或编辑在UI线程上运行的任何东西,你 无法在preExecute()或postExecute()上执行“doInBackground()”方法,或者 在Runnable对象中运行它(在doInBackground()方法内),但是通过调用runOnUI(myRunnable);

希望这会有所帮助,而且我现在无法记住方法名称,只需CTRL + SPACE就可以帮助您的IDE:D

答案 1 :(得分:0)

您已经在构造函数中执行此操作

AsyncTaskWebServiceCaller MyTask = new AsyncTaskWebServiceCaller(
                LoginActivity.this, progressDialog,
                getApplicationContext());

那是在那里传递值

您也传递了两次上下文LoginActivity.this为您提供了contextactivity,因此您无需使用getApplicationContext()。建议你永远不要使用getApplicationContext()

修改

如果您想要上下文,那么您需要做的就是

public AsyncTaskWebServiceCaller(Context context,ProgressDialog progressDialog) {
    super();
    this.progressDialog = progressDialog;
    this.context = context;
}

您不需要活动

要使用共享首选项,您需要做的就是

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);