创建一个显示wifi可用连接的应用程序

时间:2017-01-18 10:54:24

标签: android

我正在尝试创建一个能够显示Wifi连接的应用

package com.example.amit.ak503;
import java.util.List;
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.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.List;


public class MainActivity extends AppCompatActivity {
static int LENGTH_SHORT=600;

Switch s1;
WifiManager obj1;
WifiReceiver obj2;//obj of parent broadcastReceiver class
StringBuilder sb=new StringBuilder();
private final Handler handler=new Handler();
@Override
public  void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    obj1=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    s1=(Switch)findViewById(R.id.switch1);
    s1.setChecked(true);
    obj2=new WifiReceiver();
    registerReceiver(obj2,new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
   s1.setOnClickListener(new
                                 View.OnClickListener() {
                                     @Override
                                     public void onClick(View v) {

                                         if(s1.isChecked())
                                         {
                                             obj1.setWifiEnabled(true);

Toast.makeText(MainActivity.this,"Wifi enabled",Toast.LENGTH_SHORT).show();
                                         }
                                         else
                                         {
                                             obj1.setWifiEnabled(false);

Toast.makeText(MainActivity.this,"Wifi Disabled",Toast.LENGTH_SHORT).show();
                                         }
                                         doInback();
                                     }
                                 });

}
public void doInback()
{
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            obj1=(WifiManager)getSystemService(Context.WIFI_SERVICE);
            obj2=new WifiReceiver();
            registerReceiver(obj2,new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            obj1.startScan();
            doInback();
        }
    },1000);
}
public boolean onCreateOptionsMenu(Menu menu)
{
    menu.add(0,0,0,"Refresh");
    return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{//replaced onMenuItemSelected(featuredid,MenuItem item) with 
onOptionsItemSelected(MenuItem item)
    obj1.startScan();

Toast.makeText(MainActivity.this,"Refreshing",Toast.LENGTH_SHORT).show();
    return super.onOptionsItemSelected(item);
}
@Override
protected void onPause()
{
    unregisterReceiver(obj2);
    super.onPause();
    Toast.makeText(MainActivity.this,"Unregistering 
Receiver",Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume()
{
    registerReceiver(obj2,new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
    Toast.makeText(MainActivity.this,"Registering 
Receiver",Toast.LENGTH_SHORT).show();
}
class WifiReceiver extends BroadcastReceiver
{
    public void onReceive(Context c,Intent intent)
    {
        //Toast.makeText(MainActivity.this,"onReceive 
Started",Toast.LENGTH_SHORT).show();
        ArrayList<String> connections=new ArrayList<String>();
        ArrayList<Float> Signal_strength=new ArrayList<Float>();

sb=new StringBuilder();
        List<ScanResult> wifiList;
        wifiList=obj1.getScanResults();
        for(int i=0;i<wifiList.size();i++)
        {
            connections.add(wifiList.get(i).SSID);
        }


    }

    }

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: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.example.amit.ak503.MainActivity">

<Switch
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Switch"
    android:id="@+id/switch1"
    android:layout_alignParentStart="true" />

</RelativeLayout>



}

这个问题是它没有可用的wificonnections。按钮工作正常。

1 个答案:

答案 0 :(得分:0)

只需通过应用程序检查网络连接。