Internet打开后不显示Toast

时间:2014-07-02 18:13:57

标签: android android-activity oncreate

在我的OnCreate方法中,我正在检查我的互联网连接。如果没有Internet连接,我会显示一个对话框,指出" 用户没有互联网"。现在,在打开互联网后,我希望再次调用onCreate并重新创建活动。

请注意:我通过下拉Android的顶部栏并直接打开Wifi来打开互联网。我怎样才能做到这一点?我应该创建广播接收器,因为在执行此操作时未调用OnResumeOnRestart

请帮助。

这是我的网络检查班

public class NetWorkCheck {

    public NetWorkCheck(){

    }

    public static boolean hasConnection() {
        ConnectivityManager cm = (ConnectivityManager) Getit.getContext().getSystemService(
            Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetwork != null && wifiNetwork.isConnected()) {
          return true;
        }

        NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetwork != null && mobileNetwork.isConnected()) {
          return true;
        }

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
          return true;
        }

        return false;
      }
}

这绝对是完美的。

在我的onCreate中,我这样做

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    Show dialog //I want this dialog to be vanished when user switched on the wifi.and activity should be re created
}

更新2

目前,我正在使用一个广播接收器。

我在Dialog之后拨打电话: - >

     else{
        new InternetNetworkHandle(SplashActivity.this).show();
        registerReceiver(                   
                      new ConnectivityChangeReceiver(SplashActivity.class),                     
                      new IntentFilter(                 
                            ConnectivityManager.CONNECTIVITY_ACTION));


    }

BroadCast Receiver类

  public class ConnectivityChangeReceiver 
    extends BroadcastReceiver {

        String classname;

        public ConnectivityChangeReceiver(String classname){
            this.classname = classname;
        }

        @Override
        public void onReceive(Context context, Intent intent) {     
            ConnectivityManager conMan = (ConnectivityManager)     context.getSystemService(Context.CONNECTIVITY_SERVICE); 
            NetworkInfo netInfo = conMan.getActiveNetworkInfo();
            if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                Log.d("WifiReceiver", "Have Wifi Connection");
               act.finish();
               Intent refresh = new Intent(context, ???); :-> Here how can i give my that activity name ? and i don't want hardcode.
               startActivity(refresh);

            }
            else
                Log.d("WifiReceiver", "Don't have Wifi Connection");    
        }   
    };

2 个答案:

答案 0 :(得分:0)

link应该向您显示如何注册互联网状态更改意图。这会让你联系起来,你就能听到那个事件。

您可以将广播接收器放在Activity类中,并使用 getApplicationContext()来获取活动的参考。

在活动中,你想要执行其中一个来刷新你的活动。

您可以尝试刷新您的活动:

Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish();

或者这个:

finish();
startActivity(getIntent());

从这里: Reload activity in Android

您可以这样称呼它:

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    finish();
    startActivity(getIntent());
}

net = new NetWorkCheck();

if(net.hasConnection()){
    Do task
}else{
    Intent refresh = new Intent(this, clsMainUIActivity.class);
    startActivity(refresh);
    this.finish();
}

答案 1 :(得分:0)

试试吧......

finish();
startActivity(getIntent());

或者您可以使用

Activity.recreate();

如果您定位 API11 或更高版本