5秒ごとにWiFiの状態を更新するアプリを作成しました。これが主なアクティビティです
public class WiFiList extends Activity {
/** Called when the activity is first created. */
private String screen;
private ListView nets;
private WifiManager wifi;
private NetAdapter nAdap;
SharedPreferences getPrefs;
Updater skaner;
List<ScanResult> scan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String screen = getPrefs.getString("start", "list");
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(screen.equals("list")){
setContentView(R.layout.main);
nets = (ListView) findViewById(R.id.lvNets);
wifi.startScan();
scan = new ArrayList<ScanResult>();
nAdap = new NetAdapter(getApplicationContext(), scan);
nets.setAdapter(nAdap);
nAdap.notifyDataSetChanged();
}
skaner = new Updater();
skaner.execute();
}
public void refresh(){
nAdap.notifyDataSetChanged();
}
class Updater extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// doSomething();
while(wifi.isWifiEnabled()){
int waiter = getPrefs.getInt("intervals", 5) * 1000;
wifi.startScan();
scan = wifi.getScanResults();
publishProgress();
try {
Thread.sleep(waiter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
refresh();
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
skaner.cancel(true);
super.onBackPressed();
}
ただし、ListViewは更新されません。デバッガーを実行すると、それrefresh()
が呼び出され、リストの値が毎回異なることがわかります。何がそれをリフレッシュするのを妨げますか?でネットワークをスキャンonCreate()
すると、リストに表示されるので、アダプターは正しいと思いますが、とにかくコードは次のとおりです。
public class NetAdapter extends BaseAdapter{
private LayoutInflater mInflater;
List<ScanResult> res;
public NetAdapter(Context context, List<ScanResult> l){
mInflater = LayoutInflater.from(context);
res = l;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return res.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.netitem, null);
holder = new ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvFreq = (TextView) convertView.findViewById(R.id.tvFreq);
holder.tvRssi = (TextView) convertView.findViewById(R.id.tvRssi);
holder.ivState = (ImageView) convertView.findViewById(R.id.ivSignal);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(res.get(position).SSID +" (" + res.get(position).BSSID + ")");
holder.tvFreq.setText(Integer.toString(res.get(position).frequency)+"MHz");
holder.tvRssi.setText(Integer.toString(res.get(position).level)+"dBm");
holder.ivState.setImageResource(R.drawable.ic_launcher);
return convertView;
}
static class ViewHolder{
TextView tvName;
TextView tvFreq;
TextView tvRssi;
ImageView ivState;
}