繁体   English   中英

如何从一个活动转移到活动之外的另一个活动

[英]how to move from an activity to another one from outside an activity android

我想知道是否有办法通过在您想要离开的活动之外提供订单来移动当前活动。

我告诉你我得到了什么:

这是我的splashScreen活动:

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 ...

我想要的是从我的splashScreen转移到好的,取决于我从服务器得到的答案...我想在postExecute方法中给出该顺序..

有没有办法做到这一点 ?

谢谢 !! ;)

将上下文传递给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。 通过以下链接检查黑带的答案

如何从AsyncTask返回一个布尔值?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM