本文主要是介绍异步处理(Handler)线程(ThreadRunnable)定时器(Timer)Wifi管理(WifiManagerWifiInfo),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Handler.post(Runnable)方法的执行原理:http://blog.csdn.net/vampire0072009/article/details/43273375
Java中Runnable和Thread的区别:http://developer.51cto.com/art/201203/321042.htm
定时任务:Java中Timer和TimerTask的使用 :http://batitan.iteye.com/blog/253483
java.util.Timer浅析:http://www.cnblogs.com/jinspire/archive/2012/02/10/2345256.html
WifiManager解析:http://blog.csdn.net/hb8676086/article/details/50929953
代码分析
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.util.Log;
import com.android.internal.telephony.Phone;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.content.Context;
import android.content.res.Resources;import android.view.Window;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.net.wifi.WifiInfo;import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
import java.util.concurrent.Exchanger;import android.os.SystemProperties;public class WifiMAC extends EmActivity {Button okButton;TextView mContent;View btn_pass;String wifimac="waiting...";Button scan;boolean closewifi=false;WifiInfo wifiInfo=null;int i=0;Timer timer=null;private WifiManager mWifiManager;private static final String LOG_TAG="EMODE_WIFIMAC";private boolean DBG=true;protected void log(String msg){if(DBG) Log.e(LOG_TAG,msg);}final Handler mHandler=new Handler();//这个Handler对象在主线程中进行UI更新//这里虽然重写了接口Runnable的run方法但是并没有开启新线程,// 这个对象将会通过调用Handler.post(Runnable)方法,在主线程中执行updateUI操作final Runnable mUpdateResults=new Runnable() {@Overridepublic void run() {updateUI();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.em_wifimac);mContent=(TextView)findViewById(R.id.imei_content);mContent.setText(getString(R.string.wifi_mac)+wifimac);//开启一个新线程用来获取wifiMAC地址,因为WIFI的启动比较耗时,最好放到非主线程中执行//这里通过mHandler.postDelayed(mUpadateResults,5000);在主线程中更新UInew Thread(){public void run(){getWifiMAC();mHandler.postDelayed(mUpdateResults,5000);}}.start();scan=(Button)findViewById(R.id.scan);scan.setText(R.string.btn_text_scan);scan.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setAction("android.settings.WIFI_SETTINGS");//打开系统wifi设置页面startActivity(intent);}});scan.setVisibility(View.GONE);okButton=(Button)findViewById(R.id.Button01);okButton.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){finish();//结束活动}});okButton.setText("OK");}@Overrideprotected void onResume() {if(btn_pass==null){View dv=getWindow().getDecorView();btn_pass=dv.findViewById(R.id.btn_succ);btn_pass.setVisibility(View.GONE);}super.onResume();}void getWifiMAC(){mWifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);if(!mWifiManager.isWifiEnabled()){//如果WIFI不使能mWifiManager.setWifiEnabled(true);//使能wificlosewifi=true;try{//进行线程操作时需要捕获InterruptedException异常Thread.sleep(10000);//当前线程休眠10秒}catch (InterruptedException e){e.printStackTrace();}}//WifiManager的操作需要权限android.permission.CHANGE_WIFI_STATEwifiInfo=mWifiManager.getConnectionInfo();if(wifiInfo==null){try{timer=new Timer();TimerTask task=new TimerTask() {@Overridepublic void run() {try{Thread.sleep(1000);//当前线程休眠1秒wifiInfo=mWifiManager.getConnectionInfo();//获取wifi信息,需要权限i++;}catch (Exception e){e.printStackTrace();}}};timer.schedule(task,1000);//TimerTask又开启了一个新线程获取wifi信息}catch (Exception e){e.printStackTrace();}if(wifiInfo==null && i>5){//如果获取五次wifi信息后信息仍为空timer.cancel();//取消定时器任务wifimac=getString(R.string.unknow);}if(wifiInfo!=null){wifimac=wifiInfo.getMacAddress();timer.cancel();}}else{wifimac=wifiInfo.getMacAddress();}}private void updateUI(){mContent.setText(getString(R.string.wifi_mac)+wifimac);List<ScanResult> list=mWifiManager.getScanResults();int size=list.size();TextView tv_list=(TextView)findViewById(R.id.device_list);for(int i=0;i<size;i++){tv_list.append(list.get(i).SSID+"\n");}if(size>0){if(btn_pass==null){View dv=getWindow().getDecorView();btn_pass=dv.findViewById(R.id.btn_succ);}btn_pass.setVisibility(View.VISIBLE);}}@Overrideprotected void onDestroy() {super.onDestroy();Log.e("wifi","ondestroy begin");if(closewifi){mWifiManager.setWifiEnabled(false);}}
}
这篇关于异步处理(Handler)线程(ThreadRunnable)定时器(Timer)Wifi管理(WifiManagerWifiInfo)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!