Android检查网络连接选项卡加载器

时间:2012-11-01 12:53:58

标签: android

我想在加载选项卡之前实现检查网络连接。谁能告诉我它是如何完成的?以下是我的主要活动的代码。我仍然无法弄清楚它是如何完成的。请帮我。

谢谢

public class MainActivity extends SherlockFragmentActivity {
        Tab tab;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            if (!isNetworkAvailable()) {
        Toast.makeText(getApplicationContext(),
                "Internet Connection is Not Available", 10).show();
        finish();
    } else { 

    ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        tab = actionBar.newTab().setText("New Games")
                .setTabListener(new NewFragment())
                .setIcon(R.drawable.android);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setText("TOP Games")
                .setTabListener(new TopFragment())
                .setIcon(R.drawable.apple);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setText("BEST Games")
                .setTabListener(new BestFragment())
                .setIcon(R.drawable.reader);
        actionBar.addTab(tab);

}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

    }

LOGCAT:

11-01 21:55:35.023: E/AndroidRuntime(24710): FATAL EXCEPTION: main
11-01 21:55:35.023: E/AndroidRuntime(24710): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.ClassNotFoundException: in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.actionbarsherlocknavtab-1.apk]
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.os.Looper.loop(Looper.java:130)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at java.lang.reflect.Method.invoke(Method.java:507)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at dalvik.system.NativeStart.main(Native Method)
11-01 21:55:35.023: E/AndroidRuntime(24710): Caused by: java.lang.ClassNotFoundException: in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.actionbarsherlocknavtab-1.apk]
11-01 21:55:35.023: E/AndroidRuntime(24710):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-01 21:55:35.023: E/AndroidRuntime(24710):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
11-01 21:55:35.023: E/AndroidRuntime(24710):    ... 11 more

2 个答案:

答案 0 :(得分:5)

在mainActivity Oncreate Method中记下此代码。

if (!isNetworkAvailable()) {
            Toast.makeText(getApplicationContext(),
                    "Internet Connection is Not Available", 10).show();
            finish();
        } else { 

        ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            tab = actionBar.newTab().setText("New Games")
                    .setTabListener(new NewFragment())
                    .setIcon(R.drawable.android);
            actionBar.addTab(tab);

            tab = actionBar.newTab().setText("TOP Games")
                    .setTabListener(new TopFragment())
                    .setIcon(R.drawable.apple);
            actionBar.addTab(tab);

            tab = actionBar.newTab().setText("BEST Games")
                    .setTabListener(new BestFragment())
                    .setIcon(R.drawable.reader);
            actionBar.addTab(tab);

    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

答案 1 :(得分:0)

public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         //Toast.makeText(context, "No Internet Connection", 1000);
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
 }

   if(CheckNetwork.isInternetAvailable(YourActivity.this))
    {
        //Load Tabs
     }

我不确定这是不是你要找的。