安卓定时获得wifi强度编程

下面是RT的代码。知识点主要涉及wifiManager和timertask吧,其实也没什么的。

package com.example.wifistrength;

import java.util.Timer;
import java.util.TimerTask;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private WifiManager wm;
	private WifiInfo info;
	private TextView tv;
	private Timer timer;
	
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage (Message msg)
		{
			int data = msg.arg1;
			String str = String.valueOf(data);
			tv.setText(str);
		}
	};
	
	private TimerTask task = new TimerTask()
	{
		@Override
		public void run()
		{
			wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
			info = wm.getConnectionInfo();
			int t = info.getRssi();
			Message message = Message.obtain();
			message.arg1 = t;
			handler.sendMessage(message);
		}
	};
	    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		tv = (TextView)this.findViewById(R.id.text);
		
		timer = new Timer(true);
		timer.schedule(task, 0, 10);
	}
	
}


你可能感兴趣的:(安卓,定时器,wifi)