从asyncTask doInBackground调用一个Activity

时间:2015-10-29 06:11:24

标签: android android-intent android-asynctask

你好我从一个单独的base activity class调用asyncTask class,当我在一个设备上使用android 4.4.2 运行应用程序时它工作正常但是当我运行它时手机有 android 5.0 然后在此时从asyncTask应用程序崩溃调用活动。请告诉我们是否有解决方案?

public abstract class AbstractGetNameTask extends AsyncTask<Void, Void,Void>  
{
private static final String TAG = "TokenInfoTask";
protected ActivityLogin mActivity;
public static String GOOGLE_USER_DATA="No_data";
protected String mScope;
protected String mEmail;
protected int mRequestCode;
private ProgressDialog pDialog;
 private Context mContext;



AbstractGetNameTask(ActivityLogin activity, String email, String      
scope,Context context) {
    this.mActivity = activity;
    this.mScope = scope;
    this.mEmail = email;
    this.mContext=context;

}   

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    pDialog = new ProgressDialog(mContext);
    pDialog.setTitle("Loading....");
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.setMax(100);
    pDialog.show();
    super.onPreExecute();
}




@Override
protected Void doInBackground(Void... params) {
try {
fetchNameFromProfileServer();   // here in this method i start new activity

    } catch (IOException ex) {
        onError("Following Error occured, please try again. "
                + ex.getMessage(), ex);
    } catch (JSONException e) {
        onError("Bad response: " + e.getMessage(), e);
    }
    return null;
}
}

我的机器人清单有

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

但它从来没有打扰过我的其他应用程序

1 个答案:

答案 0 :(得分:0)

您应该从onPostExecute开始活动。您只需要从fetchNameFromProfileServer()返回结果,而不是从那里开始活动,并将结果传递给onPostExecute,然后开始您的活动。