0

我正在使用以下教程创建应用程序

http://www.coderzheaven.com/2011/05/09/android-tabbars-example/

对于每个选项卡,我都添加了一个带有 webview 的布局。我想检查每个选项卡更改的网络可用性,如果网络连接不可用,我已将页面重定向到 errorActivity。

import android.app.TabActivity;
import android.content.*;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MyView extends TabActivity implements OnTabChangeListener
{   
    TabHost tabHost;
    String value;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources(); 
        tabHost = (TabHost)findViewById(android.R.id.tabhost);


        TabSpec FirstTabSpec = tabHost.newTabSpec("tab1");
        TabSpec SecondTabSpec = tabHost.newTabSpec("tab2");
        TabSpec ThirdTabSpec = tabHost.newTabSpec("tab3");
        TabSpec FourthTabSpec = tabHost.newTabSpec("tab4");


        FirstTabSpec.setIndicator("Tab1",res.getDrawable(R.drawable.tab1)).setContent(new Intent(this,FirstView.class));
        secondTabSpec.setIndicator("Tab2 ",res.getDrawable(R.drawable.tab2)).setContent(new Intent(this,SecondView.class));
        ThirdTabSpec.setIndicator("Tab3",res.getDrawable(R.drawable.tab3)).setContent(new Intent(this,ThirdView.class));
        FourthTabSpec.setIndicator("Tab4",res.getDrawable(R.drawable.tab4)).setContent(new Intent(this,FourthView.class));


        tabHost.addTab(FirstTabSpec);
        tabHost.addTab(SecondTabSpec);
        tabHost.addTab(ThirdTabSpec);
        tabHost.addTab(FourthTabSpec);
        tabHost.setOnTabChangedListener(this);
    }

    public void onTabChanged(String tabId) 
    {   
        if(!IsNetworkAvaialble())
        {

                webview.stopLoading();
                Intent myIntent = new Intent((Activity)MyView.this, NetErrorPage.class);   
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ((Activity)MyView.this).startActivity(myIntent);
            finish();
        }

    }

}

但是单击选项卡,页面会在显示“网页不可用”的 2 或 3 秒之前重定向到错误页面。如何阻止这种情况?

4

2 回答 2

0

我已经解决了这个问题,如下所示。

在 OnTabchanged 事件中,我将一个静态布尔值设置为 true。在每个活动类中,我都包含了 loadurl 方法。现在,如果网络连接失败并且静态布尔值不等于 true,我已经更改了此设置,然后我已停止加载并显示警报。否则加载 url。

于 2012-10-08T09:42:45.947 回答
0

为访问网络添加此权限:

<uses-permission android:name="android.permission.INTERNET" />
于 2012-09-23T21:28:03.543 回答