2016-05-26 29 views
-1

我有一个主要活动。 我从另一个公共类中调用了一个方法。http post完成后的活动开始意向

// Background Task 
new Sync.SyncMYSQL().execute(); 

是在有意向启动的方式(从主活动)如果所调用的方法是完成:

的主要活动我调用此方法像这样

public class Sync { 

    static class SyncMYSQL extends AsyncTask<String, String, String> { 


     @Override 
     protected String doInBackground(String... params) { 
      // DO SOMETHING 
      return null; 
     } 


     protected void onPostExecute(String result) { 
      // DO SOMETHING 
     } 

    } 
} 
+0

使用[AsyncTask](https://developer.android.com/reference/android/os/AsyncTask.html)可能会帮助您 –

+0

您在其他活动上使用AsyncTask?并想将意图传递给另一项活动? –

+0

页面未找到... – Stack108

回答

1

YES,

在你的AsyncTask类可以覆盖onPostExecute方法还有写你的活动开始的代码

因为它doInBackground方法:)

内,您的帖子后执行执行预初始化所有可能的意图并用开关处理或者如果其他情况

样品

public class Sync { 

static class SyncMYSQL extends AsyncTask<String, String, String> { 
    int type; 
    public SyncMYSQL(int intentType){ 
     type=intentType; 
     } 

    @Override 
    protected String doInBackground(String... params) { 
     // DO SOMETHING 
     return null; 
    } 


     protected void onPostExecute(String result) { 
      switch(type){ 
      case 0: Intent in=new Intent(context,destinationActivity1); 
        startActivity(in); 
        break; 
      case 1: Intent in=new Intent(context,destinationActivity2); 
        startActivity(in); 
        break; 
      } 
     } 

    } 
} 

调用这个异步任务像完成这个任务这样

new Sync.SyncMYSQL(<integer_value>).execute(); 
+0

是的。但异步任务将执行许多其他活动 - 所以意图有所不同,每次 – Stack108

+0

我没有得到您的要求正确,但我认为这是你在寻找和编辑我的答案,如果不是请详细说明问题 – Jayanth

0

使用的界面。 1>创建界面

public interface AsyncTaskListener { 
public void onHttpResponse(String callResponse); 

    public void onError(String error, int pageId); 
} 

2>比对异步活动使用此

AsyncTaskListener listener; 

    private void sendResponse(String response) { 
     if (listener != null) { 
     listener.onResponse(response); 
     } 
    } 

而上的AsyncTask onPOST等,并在回应中使用sendResponse方法字符串输入“成功”,并在此之后实现该接口到您的第一个活动,并检查回应是否是“成功”而不是通过意图。