Faizan Mir
Faizan Mir

Reputation: 310

Is there a way to open Wifi activity in android and get a result for successful connection in android

So here is my scenario , I want the user to click a button which takes him to the wifi networks screen using

startActivityForResult(Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS),RESULT_CODE);

The user then selects a wifi network ,connects to it and the wifi setting activity closes and the user lands back into my application. How do I achieve this?

Upvotes: 2

Views: 5434

Answers (2)

Vahan Mambreyan
Vahan Mambreyan

Reputation: 325

android.provider.Settings gives Intent actions you can use to launch various settings screens (e.g., ACTION_WIFI_SETTINGS).

To work with button you must write:

yourButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
         startActivity(intent);
      }
});

For getting wifi connected callback, you must put permissions in manifest:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And put this code in activity onCreate or some method:

ConnectivityManager connectivityManager = (ConnectivityManager) 
   getSystemService(Context.CONNECTIVITY_SERVICE); 

NetworkRequest.Builder builder = new NetworkRequest.Builder();

connectivityManager.registerNetworkCallback(builder.build(),   
   new ConnectivityManager.NetworkCallback() { 
   @Override 
   public void onAvailable(Network network) { 
      //Do your work here or restart your activity
      Intent i = new Intent(this, MyActivity.class);
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
         Intent.FLAG_ACTIVITY_SINGLE_TOP);
      startActivity(i);
   } 
   @Override 
   public void onLost(Network network) {
       //internet lost
   }
}); 

Upvotes: 3

Naeem
Naeem

Reputation: 514

try this:

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

Upvotes: 0

Related Questions