启动画面活动运行两次

时间:2014-08-20 19:16:08

标签: java android http-post splash-screen

所以基本上这是从应用程序启动时开始的类,它从服务器获取serverList时显示一个启动画面,一旦它有列表,它就会启动下一个活动,问题是这个活动似乎是在下一个活动开始之前被调用两次,可能导致什么?

 public class SplashScreen extends Activity {

public static DefaultHttpClient httpClient;

private String resp;
static String res;
private String errorMsg;

 final Context context = this;
public static String cookieValue;
public static Cookie cookie;
String now_playing, earned;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_splash_screen);


    String Wlcclient = "Yes";
    String ClientType = "Android";
    String Version = "0.103";

    new PrefetchData().execute(Wlcclient,ClientType,Version);
}


    public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {


        SplashScreen.httpClient = new DefaultHttpClient();                               
        HttpPost request = new HttpPost(url);                           
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);



        HttpResponse response = SplashScreen.httpClient.execute(request);
       List<Cookie> cookies = SplashScreen.httpClient.getCookieStore().getCookies();

        cookie = cookies.get(0);
       cookieValue = "ASPSESSIONIDCQTCRACT=" + cookie.getValue();


        System.out.println("COOK" + cookieValue);
        Header[] headers = response.getAllHeaders();
        System.out.println("length" + headers.length);
        for (int i=0; i < headers.length; i++) {

            Header h = headers[i];

            System.out.println( "Header names: "+h.getName());
            System.out.println(  "Header Value: "+h.getValue());
        }

        CookieManager cookieManager = CookieManager.getInstance();




        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));


        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();



        String result = sb.toString();
        return result;

    }
    finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


 public class PrefetchData extends AsyncTask<String,Void,String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before making http calls         

    }

@Override
    protected String doInBackground(String... params) {

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("WlcClient",
                params[0]));    // params[0] contains the first value that you have passed and params[1] second value.
        postParameters.add(new BasicNameValuePair("ClientType",
                params[1]));
        postParameters.add(new BasicNameValuePair("Version",
                params[2]));

        String response = null;
        try {
            response = 
               executeHttpPost(server,
                            postParameters);
             res = response.toString();

            resp = res.replaceAll("\\s+", "");
            System.out.println(res);


            //resultt =res;
             return res;
        } catch (Exception e) {
            e.printStackTrace();
            errorMsg = e.getMessage();
        }
        return res;
 }    // end of doInBackground



    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // After completing http call
        // will close this activity and lauch main activity
        Intent i = new Intent(SplashScreen.this, LoginLayout.class);

        startActivity(i);

        // close this activity
        finish();
    }



}

}

1 个答案:

答案 0 :(得分:0)

在 SplashScreenActivity 的 onCreate() 中 我在 startActivity(intent) 之后用 onDestroy() 替换了 finish()

    
    public class SplashScreenActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash_screen);
    
    
    
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable(){
                @Override
                public void run() {
    
                    Intent intent;
    
                    intent = new Intent(SplashScreenActivity.this,MainActivity.class);
    
    
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
    
    //here we call the onDestory instead of finish()
                    onDestroy();
                }
            }, 1000);
    
        }
    }

为我工作