gpt4 book ai didi

java - 如何从 Activity 外部移动到另一个 Activity android

转载 作者:行者123 更新时间:2023-12-02 06:16:41 25 4
gpt4 key购买 nike

我想知道是否有办法通过在您想要离开的 Activity 之外发出命令来移动当前 Activity 。

我向你展示我得到了什么:

这是我的启动屏幕 Activity :

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类中,我当然可以移动到另一个 Activity ,但这是一个助手,我的意思是我必须从其他地方再次使用它,我当然不这样做不想再经过我的启动画面...

我想要的是从我的启动屏幕转移到好的启动屏幕,具体取决于我从服务器获得的答案...我想在 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));

另一种选择是使用接口(interface)作为对 Activity 的回调,然后从 Activity 本身启动 Activity 。在下面的链接中查看 blackbelt 的答案

How do I return a boolean from AsyncTask?

关于java - 如何从 Activity 外部移动到另一个 Activity android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com