2014-01-27 203 views
0

我想知道是否有辦法通過在您想離開的活動外提供訂單來移動當前活動。如何從一項活動轉移到另一項活動android

我告訴你什麼是我的了:

這裏是我的閃屏活動:

public class SplashScreen extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash_screen); 
    SQLHelper sqlHelper = new SQLHelper(this); 
    InfosModel infos = sqlHelper.getInfos(); 
    RequestModel request = new RequestModel(); 
    if (infos.getToken().length() == 0){ 
     request.setType("password"); 
     new OAuthHelper().execute(request); 
    } 
} 
} 

這裏是我的OAuthHelper:

public class OAuthHelper extends AsyncTask<RequestModel, Void, TokenModel> { 

public OAuthHelper(){ 
} 

@Override 
protected void onPreExecute(){ 
} 

@Override 
protected void onPostExecute(TokenModel models){ 
    if (models == null){ 
     Log.i("infos", "You have no internet connection or the server is unreachable !"); 
    } 
    else{ 
     Log.i("infos", "request's done with success !"); 
    } 

} 

@Override 
protected TokenModel doInBackground(RequestModel... request) { 
    try 
    { 
     try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://api.listopresto.com/oauth/token"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      if (request[0].getType() == "password"){ 
       /* Some Params */ 
      } 
      else{ 
       return (null); 
      } 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
      HttpResponse response = httpclient.execute(httppost); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      String s = reader.readLine(); 
      Log.i("infos", s); 
      Gson gson = new Gson(); 
      TokenModel tokenObj; 
      tokenObj = gson.fromJson(s, new TypeToken<TokenModel>(){}.getType()); 
      return (tokenObj); 
     } catch (ClientProtocolException e) { 
      Log.i("infos", "first"); 
      return (null); 
     } catch (IOException e) { 
      Log.i("infos", "second"); 
      return (null); 
     } 
    } 
    catch (Exception e){ 
     Log.i("infos", "third"); 
     return (null); 
    } 
} 
} 

如果我粘貼spashscreen類中幫助我的,當然我可以移動到另一個活動,但這是一個助手,我的意思是我必須從別的地方再次使用它,我當然不想再通過我的splashScreen ...

...我想給在postExecute方法爲了我想從我的濺射屏幕移動到好的,這取決於我的答案從服務器得到什麼..

是有辦法做到這一點?

謝謝你!! ;)

+0

http://developer.android.com/reference/android/os/Handler.html嘗試處理 – CoolMonster

回答

2

傳遞上下文的AsyncTask

的構造
new OAuthHelper(SplashScreen.this).execute(request); 

然後

Context context; 
public OAuthHelper(Context context){ 
    this.context= context; 
} 

不要在doInbackground後臺工作。返回結果doInbackground

然後在onPostExecute基於所述響應

context.startActivity(new Intent(context,SecondActivity.class)); 

另一種選擇是使用一個接口作爲呼叫回活動,然後從startActivity活動本身。通過檢查黑帶答案在下面的鏈接

How do I return a boolean from AsyncTask?

+1

很好的答案。但是,還是不要更好地使用構造函數來使用OAuthHelper而不是使用onPreExecute? – Blackbelt

+0

@blackbelt謝謝指出。複製粘貼錯誤。 – Raghunandan

+0

謝謝你拉古南丹,謝謝黑帶。你兩個搖滾;) – bottus

相關問題