Kamis, 10 Mei 2018

Cara Membuat Aplikasi Wifi Menggunakan Android


WIFI 
ActitivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.wildan.bluetooth_wifi.MainActivity">

    <
ImageView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/imageView"
       
android:src="@drawable/gambar"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true" />

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="76dp"
       
android:text="Enable Wifi"
       
android:layout_above="@+id/button2"
       
android:layout_centerHorizontal="true"
       
android:layout_marginBottom="25dp" />

    <
Button
       
android:id="@+id/button2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Disable Wifi"
       
android:layout_below="@+id/imageView"
       
android:layout_alignStart="@+id/button1" />

</
RelativeLayout>
      MainActivity.java
package com.wildan.bluetooth_wifi;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    Button
enableButton,disableButton;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
enableButton=(Button)findViewById(R.id.button1);
       
disableButton=(Button)findViewById(R.id.button2);
       
enableButton.setOnClickListener(new OnClickListener(){
           
public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.
WIFI_SERVICE);
                wifi.setWifiEnabled(
true);
            }
        });
       
disableButton.setOnClickListener(new OnClickListener(){
           
public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.
WIFI_SERVICE);
                wifi.setWifiEnabled(
false);
            }
        });
    }
}
           Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.wildan.bluetooth_wifi">
    <
uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <
uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">
        <
activity android:name=".MainActivity">
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />
                <
category android:name="android.intent.category.LAUNCHER" />
            </
intent-filter>
        </
activity>
    </
application>
</
manifest>

APLIKASI YANG TELAH DIBUAT