SplashScreen下载网站并将其传递给webview

时间:2014-08-06 09:12:12

标签: android webview android-asynctask

我想用启动画面活动打开应用程序并下载我网站的主页。下载完成后,我想启动MainActivity,将webssite传递给webview并显示它。 我尝试了一些不同的方法但没有任何作用 我的当前版本在MainActivity启动时加载webview,并且在加载网站时看起来不太好。 你有什么想法?

public class SplashScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen_layout);
        try {
            url = new URL(link);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new WczytywaczUrl().execute(url);
    }
    //asynctask class and methods
    @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Log.d(TAG, result.toString());
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            intent.putExtra("strona", result);
            startActivity(intent);
            finish();
        }


    public class MainActivity extends Activity implements OnClickListener{

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.d(TAG, "metoda onCreate");
        setContentView(R.layout.activity_main);
        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);

        Intent i = getIntent();
        String strona = i.getStringExtra("strona");
        Log.d(TAG, "strona: " + strona.toString());
        webView.loadDataWithBaseURL(link, strona, "text/html", "UTF-8", null);
    }
    }

1 个答案:

答案 0 :(得分:0)

如何用另一种布局替换“活动”呢?

<FrameLayout>
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Layout1
        android:layout_width="match_parent"
        android:layout_height="match_parent">
           // Layout contents that origin from your splash activity layout
    </Layout1>
    <Layout2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible">
           // Layout contents that origin from your main activity layout
    </Layout2>
</FrameLayout>

WebViewClient有“public void onPageFinished(WebView view, String url)”回调。

如果您实现的代码可以更改该回调中Layout1和Layout2的可见性, 然后可以在完成加载后显示webview。