How to enable wifi in android?

AndroidMobile DevelopmentApps/Applications

<p>This example demonstrate about How to enable wifi in android.</p><p><strong>Step 1</strong> &minus; Create a new project in Android Studio, go to File &rArr; New Project and fill all required details to create a new project.</p><p><strong>Step 2</strong> &minus; Add the following code to res/layout/activity_main.xml.</p><pre class="prettyprint notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &nbsp; &nbsp;xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot; &nbsp; &nbsp;xmlns:tools=&quot;http://schemas.android.com/tools&quot; &nbsp; &nbsp;android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp;android:gravity=&quot;center&quot; &nbsp; &nbsp;android:layout_height=&quot;match_parent&quot; &nbsp; &nbsp;tools:context=&quot;.MainActivity&quot;&gt; &nbsp; &nbsp;&lt;TextView &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/text&quot; &nbsp; &nbsp; &nbsp; android:textSize=&quot;30sp&quot; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;match_parent&quot; /&gt; &lt;/LinearLayout&gt;</pre><p>In the above code, we have taken a text view to enable wifi.</p><p><strong>Step 3</strong> &minus; Add the following code to src/MainActivity.java</p><pre class="prettyprint notranslate">package com.example.myapplication; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { &nbsp; &nbsp;TextView textView; &nbsp; &nbsp;@RequiresApi(api = Build.VERSION_CODES.N) &nbsp; &nbsp;@Override &nbsp; &nbsp;protected void onCreate(Bundle savedInstanceState) { &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState); &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main); &nbsp; &nbsp; &nbsp; textView = findViewById(R.id.text); &nbsp; &nbsp; &nbsp; textView.setText(&quot;enable wifi&quot;); &nbsp; &nbsp; &nbsp; textView.setOnClickListener(new View.OnClickListener() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wifiManager.setWifiEnabled(true); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp;} }</pre><p><strong>Step 4</strong> &minus; Add the following code to AndroidManifest.xml</p><pre class="prettyprint notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;com.example.myapplication&quot;&gt; &nbsp; &nbsp;&lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt; &nbsp; &nbsp;&lt;uses-permission android:name=&quot;android.permission.CHANGE_WIFI_STATE&quot; /&gt; &nbsp; &nbsp;&lt;application &nbsp; &nbsp; &nbsp; android:allowBackup=&quot;true&quot; &nbsp; &nbsp; &nbsp; android:icon=&quot;@mipmap/ic_launcher&quot; &nbsp; &nbsp; &nbsp; android:label=&quot;@string/app_name&quot; &nbsp; &nbsp; &nbsp; android:roundIcon=&quot;@mipmap/ic_launcher_round&quot; &nbsp; &nbsp; &nbsp; android:supportsRtl=&quot;true&quot; &nbsp; &nbsp; &nbsp; android:theme=&quot;@style/AppTheme&quot;&gt; &nbsp; &nbsp; &nbsp; &lt;activity android:name=&quot;.MainActivity&quot;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;intent-filter&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android:name = &quot;android.intent.action.MAIN&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android:name = &quot;android.net.conn.CONNECTIVITY_CHANGE&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;category android:name = &quot;android.intent.category.LAUNCHER&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/intent-filter&gt; &nbsp; &nbsp; &nbsp; &lt;/activity&gt; &nbsp; &nbsp;&lt;/application&gt; &lt;/manifest&gt;</pre><p>Let&#39;s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project&#39;s activity files and click Run <img src="https://www.tutorialspoint.com/assets/questions/media/19830/play.jpg" class="fr-fic fr-dii" width="14" height="14">&nbsp;icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen &ndash;</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/20137/enable_wifi.jpg" class="fr-fic fr-dib" style="width:278px; height:556px" width="278" height="556"></p><p>Click <a href="/android/projects/how_to_enable_wifi_in_android/MyApplication.zip" rel="nofollow" target="_blank">here</a> to download the project code</p>
raja
Updated on 29-Jun-2020 12:03:27

Advertisements