2016年6月20日 星期一

wifi app case study : Android App for detecting WiFi Signal



Layout Code: activity_main. Xml
When application is executed this is the first layout design that will run which will load width, height, load back ground details..etc.

<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"
    android:background="#000000"
    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=".MainActivity" >

    <Button android:id="@+id/btn_wifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="OFF"></Button>

    <ListView android:id="@+id/list_view_wifi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btn_wifi" >
    </ListView>

</RelativeLayout>


Layout Code: single_list. Xml
This xml file for single item view of list.

<LinearLayout 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" >

    <TextView android:id="@+id/txt_wifi_provider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="16dp"
        android:textStyle="bold"></TextView>

</LinearLayout>


Java File : MainActivity.java
MainActivity.java file in package com.mobilemerit.wifi.
Main activity file will load design form like buttons, text view, on click functionality, display text.

package com.mobilemerit.wifi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.mobilemerit.adapter.ListAdapter;
import com.mobilemerit.javafiles.ImportDialog;
import com.mobilemerit.wifi.R;

public class MainActivity extends Activity implements OnClickListener {
 Button setWifi;
 WifiManager wifiManager;
 WifiReceiver receiverWifi;
 List<scanresult> wifiList;
 List<string> listOfProvider;
 ListAdapter adapter;
 ListView listViwProvider;
 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  listOfProvider = new ArrayList</string><string>();
  
  /*setting the resources in class*/
  listViwProvider = (ListView) findViewById(R.id.list_view_wifi);
  setWifi = (Button) findViewById(R.id.btn_wifi);
  
  setWifi.setOnClickListener(this);
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  /*checking wifi connection
   * if wifi is on searching available wifi provider*/ 
  if (wifiManager.isWifiEnabled() == true) {
   setWifi.setText("ON");
   scaning();
  }
  /*opening a detail dialog of provider on click   */
  listViwProvider.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView< ?> parent, View view,
     int position, long id) {
     ImportDialog action = new ImportDialog(MainActivity.this, (wifiList.get(position)).toString());
     action.showDialog();
   }
  });
 }

 private void scaning() {
  // wifi scaned value broadcast receiver
  receiverWifi = new WifiReceiver();
  // Register broadcast receiver
  // Broacast receiver will automatically call when number of wifi
  // connections changed
  registerReceiver(receiverWifi, new IntentFilter(
    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  wifiManager.startScan();
  
 }

 /*setting the functionality of ON/OFF button*/
 @Override
 public void onClick(View arg0) {
  /* if wifi is ON set it OFF and set button text "OFF" */
  if (wifiManager.isWifiEnabled() == true) {
   wifiManager.setWifiEnabled(false);
   setWifi.setText("OFF");
   listViwProvider.setVisibility(ListView.GONE);
  } 
  /* if wifi is OFF set it ON 
   * set button text "ON" 
   * and scan available wifi provider*/
  else if (wifiManager.isWifiEnabled() == false) {
   wifiManager.setWifiEnabled(true);
   setWifi.setText("ON");
   listViwProvider.setVisibility(ListView.VISIBLE);
   scaning();
  }
 }

 protected void onPause() {
  super.onPause();
  unregisterReceiver(receiverWifi);
 }

 protected void onResume() {
  registerReceiver(receiverWifi, new IntentFilter(
    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  super.onResume();
 }

 class WifiReceiver extends BroadcastReceiver {

  // This method call when number of wifi connections changed
  public void onReceive(Context c, Intent intent) {
   wifiList = wifiManager.getScanResults();
   
   /* sorting of wifi provider based on level */
   Collections.sort(wifiList, new Comparator<scanresult>() {
    @Override
    public int compare(ScanResult lhs, ScanResult rhs) {
     return (lhs.level > rhs.level ? -1
       : (lhs.level == rhs.level ? 0 : 1));
    }
   });
   listOfProvider.clear();
   String providerName;
   for (int i = 0; i < wifiList.size(); i++) {
    /* to get SSID and BSSID of wifi provider*/
    providerName = (wifiList.get(i).SSID).toString()
      +"\n"+(wifiList.get(i).BSSID).toString();
    listOfProvider.add(providerName);
   }
   /*setting list of all wifi provider in a List*/
   adapter = new ListAdapter(MainActivity.this, listOfProvider);
   listViwProvider.setAdapter(adapter);
   
   adapter.notifyDataSetChanged();
  }
 }
}


Java File : ListAdapter.java
ListAdapter.java file in package com.mobilemerit.adapter.
ListAdapter class set every available wifi providers list in a listView.
 

package com.mobilemerit.adapter;

import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mobilemerit.wifi.R;

public class ListAdapter extends ArrayAdapter<string> {
 Activity mActivity;
 List</string><string> wifiName;
 private LayoutInflater inflater;

 public ListAdapter(Activity a, List</string><string> wifiName) {
  super(a, R.layout.single_list, wifiName);
  mActivity = a;
  inflater = LayoutInflater.from(mActivity);
  this.wifiName = wifiName;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  convertView = inflater.inflate(R.layout.single_list, parent, false);
  TextView wifiProvider = (TextView) convertView
    .findViewById(R.id.txt_wifi_provider);
  wifiProvider.setText(wifiName.get(position));
  return convertView;
 }

}

Java File : ImportDialog.java
importDialog.java file in package com.mobilemerit.javafiles.
ImportDialog class gives a dialog with full detail of wifi provider.
 
package com.mobilemerit.javafiles;

import android.app.Activity;
import android.app.AlertDialog;

public class ImportDialog {
 final CharSequence[] items = { "Take Photo From Gallery",
   "Take Photo From Camera" };
 Activity activity;
 AlertDialog dialog;
 AlertDialog.Builder builder;
 String detailProvader;

 public ImportDialog(Activity a, String detailProvader) {
  this.activity = a;
  this.detailProvader = detailProvader;
  builder = new AlertDialog.Builder(a);
 }

 public void showDialog() {

  builder.setTitle("wifi Provider Details");
  builder.setMessage(detailProvader);

  AlertDialog alert = builder.create();
  alert.show();
 }
}
 
Mainifest.xml
This is the main configuration file for wifi android project. This xml code will load required permission and main layout by default.
 

< ?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobilemerit.wifi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses -sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"></uses>

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

    <application android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.mobilemerit.wifi.MainActivity"
            android:label="@string/app_name" >
            <intent -filter>
                <action android:name="android.intent.action.MAIN"></action>

                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent>
        </activity>
    </application>

</manifest>



Ref : http://mobilemerit.com/android-app-for-detecting-wifi-signal-with-source-code/

沒有留言:

張貼留言