打开和关闭WIFI 的代码出现了问题 android
今天照着书上做打开和关闭WIFI的小例子练习,最后在模拟器上运行出现了问题,就是无法打开,""the application has stopped unexpectedly.please try again"
代码是按书上写的应该没有问题.帮忙看看...
代码是按书上写的应该没有问题.帮忙看看...
1
package
com.ex29;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.net.wifi.WifiManager;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.CheckBox;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 public class ex29 extends Activity {
15 private TextView myTextView;
16 private CheckBox myCheckBox;
17 // 创建WifiManager对象
18 private WifiManager mWifiManager01;
19
20 /** Called when the activity is first created. */
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super .onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25
26 myTextView = (TextView) findViewById(R.id.mTextView);
27 myCheckBox = (CheckBox) findViewById(R.id.mCheckbox);
28 // 以getSystemService取得WIFI_SERVICE
29 mWifiManager01 = (WifiManager) this
30 .getSystemService(Context.WIFI_SERVICE);
31
32 // 判断运行程序后WIFI是否打开或打开中
33 if (mWifiManager01.isWifiEnabled()) {
34 if (mWifiManager01.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
35 myCheckBox.setChecked( true );
36 myCheckBox.setText(R.string.str_unchecked);
37 } else {
38 myCheckBox.setChecked( false );
39 myCheckBox.setText(R.string.str_checked);
40 }
41
42 // 捕捉CheckBox的单击事件
43 myCheckBox.setOnClickListener( new OnClickListener() {
44
45 @Override
46 public void onClick(View v) {
47 // TODO Auto-generated method stub
48 // 当选取项为取消选取状态
49 if (myCheckBox.isChecked() == false ) {
50 // 尝试关闭wifi服务
51 try {
52 // 判断Wifi状态是否为已打开
53 if (mWifiManager01.isWifiEnabled()) {
54 // 关闭Wifi
55 if (mWifiManager01.setWifiEnabled( false )) {
56 myTextView
57 .setText(R.string.str_stop_wifi_done);
58 } else {
59 myTextView
60 .setText(R.string.str_stop_wifi_failed);
61 }
62 } else {
63 // wifi状态不为已打开状态
64 switch (mWifiManager01.getWifiState()) {
65 case WifiManager.WIFI_STATE_ENABLING:
66 myTextView
67 .setText(getResources()
68 .getText(
69 R.string.str_stop_wifi_failed)
70 + " : "
71 + getResources()
72 .getText(
73 R.string.str_wifi_enabling));
74 break ;
75 case WifiManager.WIFI_STATE_DISABLING:
76 myTextView
77 .setText(getResources()
78 .getText(
79 R.string.str_stop_wifi_failed)
80 + " : "
81 + getResources()
82 .getText(
83 R.string.str_wifi_diabling));
84 break ;
85 case WifiManager.WIFI_STATE_DISABLED:
86 myTextView.setText(getResources().getText(
87 R.string.str_stop_wifi_failed)
88 + " : "
89 + getResources().getText(
90 R.string.str_wifi_diabled));
91 break ;
92 case WifiManager.WIFI_STATE_UNKNOWN:
93 default :
94 myTextView.setText(getResources().getText(
95 R.string.str_stop_wifi_failed)
96 + " : "
97 + getResources().getText(
98 R.string.str_wifi_unknow));
99 break ;
100 }
101 myCheckBox.setText(R.string.str_checked);
102 }
103 } catch (Exception e) {
104 Log.i( " HIPPO " , e.toString());
105 }
106 } else if (myCheckBox.isChecked() == true ) {
107 // 尝试打开WIFi服务
108 try {
109 if ( ! mWifiManager01.isWifiEnabled()
110 && mWifiManager01.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
111 if (mWifiManager01.setWifiEnabled( true )) {
112 switch (mWifiManager01.getWifiState()) {
113 case WifiManager.WIFI_STATE_ENABLING:
114 myTextView
115 .setText(getResources()
116 .getText(
117 R.string.str_wifi_enabling));
118 break ;
119 case WifiManager.WIFI_STATE_ENABLED:
120 myTextView
121 .setText(getResources()
122 .getText(
123 R.string.str_start_wifi_done));
124 break ;
125 default :
126 myTextView
127 .setText(getResources()
128 .getText(
129 R.string.str_start_wifi_failed)
130 + " : "
131 + getResources()
132 .getText(
133 R.string.str_wifi_unknow));
134 break ;
135 }
136 } else {
137 myTextView
138 .setText(R.string.str_start_wifi_failed);
139 }
140 } else {
141 switch (mWifiManager01.getWifiState()) {
142 case WifiManager.WIFI_STATE_ENABLING:
143 myTextView
144 .setText(getResources()
145 .getText(
146 R.string.str_start_wifi_failed)
147 + " : "
148 + getResources()
149 .getText(
150 R.string.str_wifi_enabling));
151 break ;
152 case WifiManager.WIFI_STATE_DISABLING:
153 myTextView
154 .setText(getResources()
155 .getText(
156 R.string.str_start_wifi_failed)
157 + " : "
158 + getResources()
159 .getText(
160 R.string.str_wifi_diabling));
161 break ;
162 case WifiManager.WIFI_STATE_DISABLED:
163 myTextView.setText(getResources().getText(
164 R.string.str_start_wifi_failed)
165 + " : "
166 + getResources().getText(
167 R.string.str_wifi_diabled));
168 break ;
169 case WifiManager.WIFI_STATE_UNKNOWN:
170 default :
171 myTextView.setText(getResources().getText(
172 R.string.str_start_wifi_failed)
173 + " : "
174 + getResources().getText(
175 R.string.str_wifi_unknow));
176 break ;
177 }
178 }
179 myCheckBox.setText(R.string.str_unchecked);
180 } catch (Exception e) {
181 Log.i( " HIPPO " , e.toString());
182 e.printStackTrace();
183 }
184 }
185
186 }
187
188 });
189 }
190
191 }
192
193 public void mMakeTextToast(String str, boolean isLong) {
194 if (isLong == true ) {
195 Toast.makeText(ex29. this , str, Toast.LENGTH_LONG).show();
196 } else {
197 Toast.makeText(ex29. this , str, Toast.LENGTH_SHORT).show();
198 }
199 }
200
201 @Override
202 protected void onResume() {
203 // TODO Auto-generated method stub
204 try {
205 switch (mWifiManager01.getWifiState()) {
206 case WifiManager.WIFI_STATE_ENABLING:
207 myTextView.setText(getResources().getText(
208 R.string.str_wifi_enabling));
209 break ;
210 case WifiManager.WIFI_STATE_ENABLED:
211 myTextView.setText(getResources().getText(
212 R.string.str_wifi_enabled));
213 break ;
214 case WifiManager.WIFI_STATE_DISABLING:
215 myTextView.setText(getResources().getText(
216 R.string.str_wifi_diabling));
217 break ;
218 case WifiManager.WIFI_STATE_DISABLED:
219 myTextView.setText(getResources().getText(
220 R.string.str_wifi_diabled));
221 break ;
222 case WifiManager.WIFI_STATE_UNKNOWN:
223 default :
224 myTextView.setText(getResources().getText(
225 R.string.str_wifi_unknow));
226 break ;
227 }
228 } catch (Exception e) {
229 myTextView.setText(e.toString());
230 e.getStackTrace();
231 }
232
233 super .onResume();
234 }
235
236 @Override
237 protected void onPause() {
238 // TODO Auto-generated method stub
239 super .onPause();
240 }
241
242 }
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.net.wifi.WifiManager;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.CheckBox;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 public class ex29 extends Activity {
15 private TextView myTextView;
16 private CheckBox myCheckBox;
17 // 创建WifiManager对象
18 private WifiManager mWifiManager01;
19
20 /** Called when the activity is first created. */
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super .onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25
26 myTextView = (TextView) findViewById(R.id.mTextView);
27 myCheckBox = (CheckBox) findViewById(R.id.mCheckbox);
28 // 以getSystemService取得WIFI_SERVICE
29 mWifiManager01 = (WifiManager) this
30 .getSystemService(Context.WIFI_SERVICE);
31
32 // 判断运行程序后WIFI是否打开或打开中
33 if (mWifiManager01.isWifiEnabled()) {
34 if (mWifiManager01.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
35 myCheckBox.setChecked( true );
36 myCheckBox.setText(R.string.str_unchecked);
37 } else {
38 myCheckBox.setChecked( false );
39 myCheckBox.setText(R.string.str_checked);
40 }
41
42 // 捕捉CheckBox的单击事件
43 myCheckBox.setOnClickListener( new OnClickListener() {
44
45 @Override
46 public void onClick(View v) {
47 // TODO Auto-generated method stub
48 // 当选取项为取消选取状态
49 if (myCheckBox.isChecked() == false ) {
50 // 尝试关闭wifi服务
51 try {
52 // 判断Wifi状态是否为已打开
53 if (mWifiManager01.isWifiEnabled()) {
54 // 关闭Wifi
55 if (mWifiManager01.setWifiEnabled( false )) {
56 myTextView
57 .setText(R.string.str_stop_wifi_done);
58 } else {
59 myTextView
60 .setText(R.string.str_stop_wifi_failed);
61 }
62 } else {
63 // wifi状态不为已打开状态
64 switch (mWifiManager01.getWifiState()) {
65 case WifiManager.WIFI_STATE_ENABLING:
66 myTextView
67 .setText(getResources()
68 .getText(
69 R.string.str_stop_wifi_failed)
70 + " : "
71 + getResources()
72 .getText(
73 R.string.str_wifi_enabling));
74 break ;
75 case WifiManager.WIFI_STATE_DISABLING:
76 myTextView
77 .setText(getResources()
78 .getText(
79 R.string.str_stop_wifi_failed)
80 + " : "
81 + getResources()
82 .getText(
83 R.string.str_wifi_diabling));
84 break ;
85 case WifiManager.WIFI_STATE_DISABLED:
86 myTextView.setText(getResources().getText(
87 R.string.str_stop_wifi_failed)
88 + " : "
89 + getResources().getText(
90 R.string.str_wifi_diabled));
91 break ;
92 case WifiManager.WIFI_STATE_UNKNOWN:
93 default :
94 myTextView.setText(getResources().getText(
95 R.string.str_stop_wifi_failed)
96 + " : "
97 + getResources().getText(
98 R.string.str_wifi_unknow));
99 break ;
100 }
101 myCheckBox.setText(R.string.str_checked);
102 }
103 } catch (Exception e) {
104 Log.i( " HIPPO " , e.toString());
105 }
106 } else if (myCheckBox.isChecked() == true ) {
107 // 尝试打开WIFi服务
108 try {
109 if ( ! mWifiManager01.isWifiEnabled()
110 && mWifiManager01.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
111 if (mWifiManager01.setWifiEnabled( true )) {
112 switch (mWifiManager01.getWifiState()) {
113 case WifiManager.WIFI_STATE_ENABLING:
114 myTextView
115 .setText(getResources()
116 .getText(
117 R.string.str_wifi_enabling));
118 break ;
119 case WifiManager.WIFI_STATE_ENABLED:
120 myTextView
121 .setText(getResources()
122 .getText(
123 R.string.str_start_wifi_done));
124 break ;
125 default :
126 myTextView
127 .setText(getResources()
128 .getText(
129 R.string.str_start_wifi_failed)
130 + " : "
131 + getResources()
132 .getText(
133 R.string.str_wifi_unknow));
134 break ;
135 }
136 } else {
137 myTextView
138 .setText(R.string.str_start_wifi_failed);
139 }
140 } else {
141 switch (mWifiManager01.getWifiState()) {
142 case WifiManager.WIFI_STATE_ENABLING:
143 myTextView
144 .setText(getResources()
145 .getText(
146 R.string.str_start_wifi_failed)
147 + " : "
148 + getResources()
149 .getText(
150 R.string.str_wifi_enabling));
151 break ;
152 case WifiManager.WIFI_STATE_DISABLING:
153 myTextView
154 .setText(getResources()
155 .getText(
156 R.string.str_start_wifi_failed)
157 + " : "
158 + getResources()
159 .getText(
160 R.string.str_wifi_diabling));
161 break ;
162 case WifiManager.WIFI_STATE_DISABLED:
163 myTextView.setText(getResources().getText(
164 R.string.str_start_wifi_failed)
165 + " : "
166 + getResources().getText(
167 R.string.str_wifi_diabled));
168 break ;
169 case WifiManager.WIFI_STATE_UNKNOWN:
170 default :
171 myTextView.setText(getResources().getText(
172 R.string.str_start_wifi_failed)
173 + " : "
174 + getResources().getText(
175 R.string.str_wifi_unknow));
176 break ;
177 }
178 }
179 myCheckBox.setText(R.string.str_unchecked);
180 } catch (Exception e) {
181 Log.i( " HIPPO " , e.toString());
182 e.printStackTrace();
183 }
184 }
185
186 }
187
188 });
189 }
190
191 }
192
193 public void mMakeTextToast(String str, boolean isLong) {
194 if (isLong == true ) {
195 Toast.makeText(ex29. this , str, Toast.LENGTH_LONG).show();
196 } else {
197 Toast.makeText(ex29. this , str, Toast.LENGTH_SHORT).show();
198 }
199 }
200
201 @Override
202 protected void onResume() {
203 // TODO Auto-generated method stub
204 try {
205 switch (mWifiManager01.getWifiState()) {
206 case WifiManager.WIFI_STATE_ENABLING:
207 myTextView.setText(getResources().getText(
208 R.string.str_wifi_enabling));
209 break ;
210 case WifiManager.WIFI_STATE_ENABLED:
211 myTextView.setText(getResources().getText(
212 R.string.str_wifi_enabled));
213 break ;
214 case WifiManager.WIFI_STATE_DISABLING:
215 myTextView.setText(getResources().getText(
216 R.string.str_wifi_diabling));
217 break ;
218 case WifiManager.WIFI_STATE_DISABLED:
219 myTextView.setText(getResources().getText(
220 R.string.str_wifi_diabled));
221 break ;
222 case WifiManager.WIFI_STATE_UNKNOWN:
223 default :
224 myTextView.setText(getResources().getText(
225 R.string.str_wifi_unknow));
226 break ;
227 }
228 } catch (Exception e) {
229 myTextView.setText(e.toString());
230 e.getStackTrace();
231 }
232
233 super .onResume();
234 }
235
236 @Override
237 protected void onPause() {
238 // TODO Auto-generated method stub
239 super .onPause();
240 }
241
242 }