Android:从Async onPostExecute方法调用main活动方法

时间:2015-05-11 09:52:35

标签: android asynchronous

尝试在onPostExecute()上调用processFinish()方法,但没有取得任何成功。我尝试了很多东西,但我认为我做错了什么。 MainActivity上的onPostExecute()方法未从//myClass.java public class myClass extends Activity { public AsyncResponse delegate=null; public myClass(Context context){ this.mContext = context; } public interface AsyncResponse { void processFinish(String output); } public class GetNotification extends AsyncTask<Integer, Void, String>{ public GetNotification() { super(); } @Override protected String doInBackground(Integer... mArgs){ //code } @Override protected void onPostExecute(String result){ super.onPostExecute(result); delegate.processFinish(result); } @Override protected void onPreExecute(){ } } public void getValue(int f){ m_flag = f; if(Build.VERSION.SDK_INT >= 11){ new GetNotification().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, f); } else{ new GetNotification().execute(f); } } } //MainActivity.java public class MainActivity extends Activity implements AsyncResponse{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //code } @Override public void processFinish(String output){ getComActivity(output); } private void getData(String mURL){ this.getUrl=new com.sample.myClass(this); getUrl.getValue(1); } public void getComActivity(String gStr){ if (gStr != null && gStr.trim() != ""){ Intent mIntent = new Intent(this.getApplicationContext(), myActivity.class); this.startActivity(mIntent); } } } 调用。

@Indexed

3 个答案:

答案 0 :(得分:1)

您需要初始化delegate。更改myClass构造函数中的代码。

public myClass(Context context){
      delegate = (AsyncResponse) context;
}

答案 1 :(得分:0)

  

public GetNotification(){                超();           }

//In your AsyncTask class
//use MainActivity or myClass instead of Activity
private Activity activity;
public GetNotification(Activity activity) {
   this.activity = activity;
}
private void callMethod()
{
   activity.some_method();
}

答案 2 :(得分:0)

public class myClass extends Activity   {

    public AsyncResponse delegate=null;
    Context mContext;


    public myClass(Context context){
          this.mContext = context;

    }

    public interface AsyncResponse {
        void processFinish(String output);
    }


 public class GetNotification extends AsyncTask<Integer, Void, String>{

     public GetNotification() {
             super();
        }


         @Override
         protected void onPreExecute(){
             //start dialog progress over here
         }
        @Override
        protected String doInBackground(Integer... mArgs){
              //code
            return null;
        }

        @Override
        protected void onPostExecute(String result){
          super.onPostExecute(result);
          delegate.processFinish(result);

        }

    }

    @SuppressLint("NewApi")
    public void getValue(int f){
       int m_flag = f;
        if(Build.VERSION.SDK_INT >= 11){
            new GetNotification().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, f);
        }
        else{
            new GetNotification().execute(f);

            if( MainActivity.activity!=null)
              MainActivity.getComActivity("PassYourStringOverHere");      //here I am giving example how to call MainActivity method from other activity


       }
    }


public class MainActivity extends Activity implements AsyncResponse{

    private myClass getUrl;
    public static Activity activity=null;
    public static Context context=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity=this;//use to initialize the activity...later you can call...and start intent using this activity object...
        context=this;//use for Intent...why I used this pattern because i made a static method so I have to do...or else it will show error...
        //code
        }

    @Override
    public void processFinish(String output){
        getComActivity(output);
    }

    private void getData(String mURL){
        this.getUrl=new com.sample.myClass(this);
        getUrl.getValue(1);


    }
    public static  void getComActivity(String gStr){
        if (gStr != null && gStr.trim() != ""){
            Intent mIntent = new Intent(context, myActivity.class);
            activity.startActivity(mIntent);
        }
    }
}