2015-09-14 2 views
1

Я использую сейчас этот код, который проверяет подключение к Интернету вкл. Или выкл. Если он выключен, отображается страница настроек беспроводной сети. Итак, я хочу, после того, как я включу Wi-Fi-соединение, он должен открыть активность SplashScreen 2. Как это сделать? Как вы увидите ниже, нет намерения/действия, чтобы попросить открыть новое действие.После включения Wi-Fi он должен открыть новую активность

public class Splash extends Activity { 
static ConnectivityManager cm; 
AlertDialog dailog; 
AlertDialog.Builder build; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking 
// internet 
build = new Builder(Splash.this); // connectivity 
setContentView(R.layout.activity_splash); 
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is 
     // there screen goes 
     // to next screen 
     // else shows 
     // message 
     .isConnectedOrConnecting() 
     || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) 
       .isConnectedOrConnecting()) { 
    Log.e("cm value", 
      "" 
        + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) 
          .isConnectedOrConnecting()); 
    Toast.makeText(Splash.this, "Internet is active", 2000).show(); 
    Thread mythread = new Thread() { 
     public void run() { 
      try { 

       sleep(5000); 

      } catch (Exception e) { 
      } finally { 
       Intent intent = new Intent(Splash.this, 
         SplashScreen2.class); 
       startActivity(intent); 
       finish(); 
      } 
     } 
    }; 
    mythread.start(); 
} else { 

    build.setMessage("This application requires Internet connection.Would you connect to internet ?"); 
    build.setPositiveButton("Yes", new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 

    `Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity` 

     } 
    }); 
    build.setNegativeButton("No", new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      build.setMessage("Are sure you want to exit?"); 
      build.setPositiveButton("Yes", new OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        finish(); 
       } 
      }); 

     } 
    }); 
    dailog = build.create(); 
    dailog.show(); 

} 

}

+0

здесь смотрите - Http: // StackOverflow .com/questions/6362314/wifi-connect-disconnect-listener – Tasos

ответ

1

Вы можете просто поместить весь код в onResume() вместо OnCreate() метод что-то вроде ниже,

public class Splash extends Activity { 
static ConnectivityManager cm; 
AlertDialog dailog; 
AlertDialog.Builder build; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_splash); 
} 
@Override 
    public void onResume() { 
    super.onResume(); 
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking 
    // internet 
    build = new Builder(Splash.this); // connectivity 
    if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is 
      // there screen goes 
      // to next screen 
      // else shows 
      // message 
      .isConnectedOrConnecting() 
      || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) 
        .isConnectedOrConnecting()) { 
     Log.e("cm value", 
       "" 
         + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) 
           .isConnectedOrConnecting()); 
     Toast.makeText(Splash.this, "Internet is active", 2000).show(); 
     Thread mythread = new Thread() { 
      public void run() { 
       try { 

        sleep(5000); 

       } catch (Exception e) { 
       } finally { 
        Intent intent = new Intent(Splash.this, 
          SplashScreen2.class); 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }; 
     mythread.start(); 
    } else { 

     build.setMessage("This application requires Internet connection.Would you connect to internet ?"); 
     build.setPositiveButton("Yes", new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 

       startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 

     `Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity` 

      } 
     }); 
     build.setNegativeButton("No", new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       build.setMessage("Are sure you want to exit?"); 
       build.setPositiveButton("Yes", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         finish(); 
        } 
       }); 

      } 
     }); 
     dailog = build.create(); 
     dailog.show(); 

    } 
} 
+0

Итак, он выглядит несколько красных маркеров по строкам, см, диалоговому интерфейсу и т. д. Несколько маркеров в этой строке \t - Builder не может быть разрешен до \t a type –

+0

Не удаляйте свои глобальные переменные, я обновляю свой ответ –

+0

Я обновил свой ответ .... Пожалуйста, проверьте, содержит ли он полную активность –