안드로이드 개발 질문/답변
(글 수 45,052)
002.
003.
import
java.util.List;
004.
005.
import
android.app.Activity;
006.
import
android.content.BroadcastReceiver;
007.
import
android.content.Context;
008.
import
android.content.Intent;
009.
import
android.content.IntentFilter;
010.
import
android.net.wifi.ScanResult;
011.
import
android.net.wifi.WifiConfiguration;
012.
import
android.net.wifi.WifiInfo;
013.
import
android.net.wifi.WifiManager;
014.
import
android.os.Bundle;
015.
import
android.util.Log;
016.
import
android.view.View;
017.
import
android.view.View.OnClickListener;
018.
import
android.widget.Button;
019.
import
android.widget.EditText;
020.
import
android.widget.TextView;
021.
import
android.widget.Toast;
022.
023.
public
class
WiFiDemo
extends
Activity
implements
OnClickListener {
024.
//private static final String TAG = "maluchi";
025.
WifiManager wifi;
026.
BroadcastReceiver receiver;
027.
028.
TextView textStatus;
029.
Button buttonScan;
030.
EditText edit;
031.
032.
/** Called when the activity is first created. */
033.
@Override
034.
public
void
onCreate(Bundle savedInstanceState) {
035.
super
.onCreate(savedInstanceState);
036.
setContentView(R.layout.main);
037.
038.
// Setup UI
039.
textStatus = (TextView) findViewById(R.id.textStatus);
040.
buttonScan = (Button) findViewById(R.id.buttonScan);
041.
edit = (EditText)findViewById(R.id.edt);
042.
buttonScan.setOnClickListener(
this
);
043.
044.
// Setup WiFi
045.
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
046.
047.
// Get WiFi status
048.
WifiInfo info = wifi.getConnectionInfo();
049.
textStatus.append(
"\n\nWiFi Status: "
+ info.toString());
050.
051.
// List available networks
052.
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
053.
for
(WifiConfiguration config : configs) {
054.
textStatus.append(
"\n\n"
+ config.toString());
055.
}
056.
057.
// Register Broadcast Receiver
058.
if
(receiver ==
null
){
059.
receiver =
new
BroadcastReceiver() {
060.
061.
@Override
062.
public
void
onReceive(Context context, Intent intent) {
063.
// TODO Auto-generated method stub
064.
List<ScanResult> results = wifi.getScanResults();
065.
ScanResult bestSignal =
null
;
066.
String message = String.format(
"%s networks found.\n"
, results.size());
067.
068.
Log.d(
"maluchi"
,
"onReceive() message: "
+ message);
069.
070.
for
(ScanResult result : results) {
071.
if
(bestSignal ==
null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) <
0
)
072.
{
073.
bestSignal = result;
074.
075.
}
076.
077.
String strStrong = String.format(
"%s is detected.\n"
, result.SSID);
078.
message +=strStrong;
079.
Log.d(
"maluchi"
,
"AP: "
+ strStrong);
080.
}
081.
082.
String msg = String.format(
"%s is the strongest."
, bestSignal.SSID);
083.
Toast.makeText(WiFiDemo.
this
, msg, Toast.LENGTH_LONG).show();
084.
085.
message +=msg;
086.
087.
print(message);
088.
}
089.
};
090.
}
091.
092.
registerReceiver(receiver,
new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
093.
Log.d(
"maluchi"
,
"onCreate()"
);
094.
}
095.
096.
@Override
097.
protected
void
onStop() {
098.
// TODO Auto-generated method stub
099.
unregisterReceiver(receiver);
100.
super
.onStop();
101.
}
102.
public
void
onClick(View view) {
103.
Toast.makeText(
this
,
"On Click Clicked. Toast to that!!!"
,
104.
Toast.LENGTH_LONG).show();
105.
106.
if
(view.getId() == R.id.buttonScan) {
107.
Log.d(
"maluchi"
,
"onClick() wifi.startScan()"
);
108.
wifi.startScan();
109.
}
110.
}
111.
112.
public
void
print(String string)
113.
{
114.
String str = edit.getText().toString();
115.
if
(str.length() >
0
)
116.
str +=
"\n\n"
;
117.
118.
str +=string;
119.
120.
edit.setText(str);
121.
122.
}
123.
}
XML 내용
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03.
android:orientation
=
"vertical"
04.
android:layout_width
=
"fill_parent"
05.
android:layout_height
=
"fill_parent"
>
06.
07.
<
Button
android:layout_width
=
"wrap_content"
08.
android:layout_height
=
"wrap_content"
android:id
=
"@+id/buttonScan"
09.
android:text
=
"Scan"
></
Button
>
10.
11.
<
TextView
android:layout_width
=
"fill_parent"
12.
android:layout_height
=
"wrap_content"
android:id
=
"@+id/textStatus"
13.
android:text
=
"WiFiDemo"
/>
14.
15.
<
EditText
16.
android:layout_width
=
"fill_parent"
17.
android:layout_height
=
"fill_parent"
18.
android:id
=
"@+id/edt"
19.
android:singleLine
=
"false"
20.
android:editable
=
"false"
21.
android:scrollbars
=
"vertical"
22.
android:gravity
=
"top|left"
23.
android:text
=
""
/>
24.
25.
</
LinearLayout
>