0

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?

Markus Kauppinen
  • 2,756
  • 4
  • 16
  • 28
Faizan Mir
  • 112
  • 1
  • 12
  • 2
    You might find what you're looking for here: [How to call Wi-Fi settings screen from my application using Android](https://stackoverflow.com/questions/2318310/how-to-call-wi-fi-settings-screen-from-my-application-using-android) – Nikos Hidalgo Feb 11 '20 at 11:12
  • But this doesn't answer my question of how to return back to the activity by connecting to a particular network – Faizan Mir Feb 11 '20 at 18:19

2 Answers2

2

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
   }
}); 
Vahan Mambreyan
  • 349
  • 2
  • 5
  • I am able to do that already ... What I want to achieve is that the user should select a wifi network and the wifi activity should close after that thus returning the user to the app. – Faizan Mir Feb 11 '20 at 18:19
  • I will try this and let you know – Faizan Mir Feb 13 '20 at 08:36
0

try this:

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
Naeem
  • 350
  • 3
  • 19