-1
クリックするとwifi結果を表示するアプリを作成するにはどうすればいいですか?クリックしてWi-Fiネットワークに接続しますか?Wi-Fiスキャン結果をクリック可能なボタンとして表示
以下のコードは、SSIDをリストとして表示しますが、クリックできません。
package com.example.amit.ak504;
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.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static int LENGTH_SHORT=600;
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;// this is an object of List
StringBuilder sb = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setupActionBar();
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mainText = (TextView) findViewById(R.id.tv1);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (mainWifi.isWifiEnabled() == false)
{
// If wifi disabled then enable it
Toast.makeText(getApplicationContext(), "wifi is disabled..making it
enabled",Toast.LENGTH_LONG).show();
mainWifi.setWifiEnabled(true);
}
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
mainText.setText("Starting Scan...");
}
// private void setupActionBar() {
// if (actionBar != null) {
// Show the Up button in the action bar.
// actionBar.setDisplayHomeAsUpEnabled(true);
// }
//}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");//side menu
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
mainWifi.startScan();
mainText.setText("Starting Scan");
return super.onOptionsItemSelected(item);
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
// Broadcast receiver class called its receive method
// when number of wifi connections changed
class WifiReceiver extends BroadcastReceiver {
// This method call when number of wifi connections changed
public void onReceive(Context c, Intent intent)
{
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
//why getScanResult is stored into a list.
//note that wifilist was a object of List<ScanResult>
sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n");
Button bt1=(Button)findViewById(R.id.button);
for(int i = 0; i < wifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ". ");
sb.append((wifiList.get(i)).SSID);
sb.append("\n\n");
}
bt1.setText(wifiList.get(2).SSID);
mainText.setText(sb);
}
}
}
これは私が達成しようとしているものです: