我想知道是否有办法通过在您想要离开的活动之外提供订单来移动当前活动。
我告诉你我得到了什么:
这是我的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方法中给出该命令..
有办法做到这一点吗?
谢谢你!! ;)答案 0 :(得分: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。通过以下链接中的黑带检查答案