打开URL,然后是第二个URL

时间:2017-10-31 07:36:42

标签: android url

我需要打开其他网址,然后删除以前的网址进行查看。需要采取措施代替关闭浏览器标签。

第一个URL正在打开,第二个URL无处可见。我哪里错了?

    Uri uri = Uri.parse(URL_STRING); 
    //modified the URL_STRING for security
    URL_STRING = "https://myserver.com/action";
    final Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
    intent1.putExtra(Browser.EXTRA_APPLICATION_ID, "toto");
    startActivity(intent1);

    //SystemClock.sleep(1000);

    String POST_URL = "http://www.google.com";
    uri = Uri.parse(POST_URL);
    final Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
    intent2.putExtra(Browser.EXTRA_APPLICATION_ID, "toto");

甚至尝试过不同价值的睡眠。没用。

1 个答案:

答案 0 :(得分:1)

按照此操作在外部浏览器中加载两个URls。

将这些变量声明为全局变量。

int count = 0;
Runnable runnable=null;
Handler handler = new Handler();

然后调用此方法在浏览器中加载Url。

  public void goToBrowser() {
    final Uri[] uri = new Uri[1];
    runnable = new Runnable() {
        public void run() {
            switch (count) {
                case 0:
                    String URL_STRING = "https://myserver.com/action";
                    uri[0] = Uri.parse(URL_STRING);
                    //modified the URL_STRING for security
                    final Intent intent1 = new Intent(Intent.ACTION_VIEW, uri[0]);
                    intent1.putExtra(Browser.EXTRA_APPLICATION_ID, "toto");
                    startActivity(intent1);
                    break;
                case 1:
                    String POST_URL = "http://www.google.com";
                    uri[0] = Uri.parse(POST_URL);
                    final Intent intent2 = new Intent(Intent.ACTION_VIEW, uri[0]);
                    intent2.putExtra(Browser.EXTRA_APPLICATION_ID, "toto");
                    startActivity(intent2);
                    break;
            }

            if (count++ <= 1){
                handler.postDelayed(this, 1000);
            }else {
                handler.removeCallbacks(runnable);
            }

        }
    };
    handler.post(runnable);
}