Android Studio Wifi Açma Kapama

Android Studio Wifi Açma Kapama

Bugün kablosuz internet erişimleri sağlayan wifi’nin çalışmasını inceliyeceğiz. Wifi akıllı telefonlarda kablosuz internet bağlantısı erişimine olanak verir. Bir toggle button ile Android Studio’da wifi kontrolü sağlayarak işlemlerimizi gerçekleştireceğiz.

Activity_main:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

 

    <ToggleButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/a"

        android:id="@+id/toggleButton"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>

Java Kodları;

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

package com.umiitkose.myapplication;

 

        import android.bluetooth.BluetoothAdapter;

        import android.content.Context;

        import android.content.Intent;

        import android.graphics.Bitmap;

        import android.media.MediaRecorder;

        import android.net.Uri;

        import android.net.wifi.WifiManager;

        import android.os.Bundle;

        import android.app.Activity;

        import android.provider.MediaStore;

        import android.view.View;

        import android.widget.Button;

        import android.widget.CheckBox;

        import android.widget.Toast;

        import android.widget.ToggleButton;

 

public class MainActivity extends Activity {

 

 

WifiManager modem=null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        final ToggleButton btn=(ToggleButton)findViewById(R.id.toggleButton);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                if(btn.isChecked()){

                   wifiAc();

                }

                else

                    wifiKapat();

 

            }

        });

}

    private void wifiAc(){

        modem=(WifiManager)getSystemService(Context.WIFI_SERVICE);

        if(modem.getWifiState()== WifiManager.WIFI_STATE_DISABLED){

            modem.setWifiEnabled(true);

            }else if(modem.getWifiState()==WifiManager.WIFI_STATE_ENABLING){

            Toast.makeText(MainActivity.this, "Açık", Toast.LENGTH_SHORT).show();

        }

 

    }

    private void wifiKapat(){

        modem=(WifiManager)getSystemService(Context.WIFI_SERVICE);

        if(modem.getWifiState()== WifiManager.WIFI_STATE_ENABLED){

            modem.setWifiEnabled(false);

        }else if(modem.getWifiState()==WifiManager.WIFI_STATE_DISABLING){

            Toast.makeText(MainActivity.this, "Zaten Kapalı", Toast.LENGTH_SHORT).show();

        }

 

    }

 

}

Android Manifest İzinlerimiz:

 

1

2

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

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

 

 

 

Henüz Yorum Yapılmamış, İlk Yorumu Siz Yapın

Yorum Yollayın