如何更新contentView

时间:2011-11-01 07:45:19

标签: android

我已从菜单中调用wifi_refresh()来自动更新wifi信息,但是,这样,显示屏上没有显示任何内容。但是,当我使用按钮刷新时,一切正常。有人帮忙吗?

    package com.wifi_info;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import android.app.Activity;
    import android.content.Context;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.view.MenuItem.OnMenuItemClickListener;
    import java.util.concurrent.*;

   public class Wifi_infoActivity extends Activity {
/** Called when the activity is first created. */

int update_freq =1;
MenuItem auto_check;
MenuItem view_cg;
MenuItem view_apl;
MenuItem view_sm;
MenuItem set_1;
MenuItem set_3;
MenuItem set_5;
MenuItem set_10;
MenuItem view_menu;
MenuItem settings_menu;
int interval = 1000;
boolean checked = false;
ScheduledThreadPoolExecutor exet = new ScheduledThreadPoolExecutor(5);// not sure          about the number  
ScheduledFuture<?> handler;
Button refreshButton;

//------------------------------------wifi_refresh---------------------------------------------
WifiManager wifi;
ArrayAdapter<String> aa;
int layoutID = android.R.layout.simple_list_item_1;
ArrayList<String> wifi_info;
int result_size;
String channel;
ListView lv;
List<ScanResult> results;

// --------------------------------------- wifi_refresh ----- ----------------------------------------

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    wifi_refresh();

    refreshButton = (Button)findViewById(R.id.refreshbutton);
    refreshButton.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){

            wifi_refresh();             
        }
    });
}

public void onStop(){
    super.onStop();
    exet.shutdownNow();
}

@Override
public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    view_menu = menu.findItem(R.id.view);
    settings_menu = menu.findItem(R.id.settings);
    auto_check=menu.findItem(R.id.set_check);
    if (checked == false)
        auto_check.setChecked(false);
    else
        auto_check.setChecked(true);
    view_cg = menu.findItem(R.id.view_cg);
    view_apl = menu.findItem(R.id.view_apl);
    view_sm = menu.findItem(R.id.view_sm);    
    set_1 = menu.findItem(R.id.set_1);
    set_3 = menu.findItem(R.id.set_3);
    set_5 = menu.findItem(R.id.set_5);
    set_10 = menu.findItem(R.id.set_10);

    view_menu.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            return true;
        }
     });

    settings_menu.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            return true;
        }
    });

    view_cg.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            //show channel graph
            return true;
        }
    });

    view_apl.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            wifi_refresh();
            return true;
        }
    });

    view_sm.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            //show sm
            return true;
        }
    });

    auto_check.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            if(checked==false){ 
                auto_check.setChecked(true);
                checked = true;
                set_1.setCheckable(true);
                set_3.setCheckable(true);
                set_5.setCheckable(true);
                set_10.setCheckable(true);
                showChosen();
                schedule_task();

            }else{
                auto_check.setChecked(false);
                checked = false;
                set_1.setCheckable(false);
                set_3.setCheckable(false);
                set_5.setCheckable(false);
                set_10.setCheckable(false);
            }
            return true;
        }
    });

    set_1.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            if(checked==true){
            showChosen();
            update_freq=1;
            showChosen();
            reschedule();
            }
            return true;
        }
    });

    set_3.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            if(checked==true){
            showChosen();
            update_freq=3;
            showChosen();
             reschedule();
            }
            return true;
        }
    });

    set_5.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            if(checked==true){
            showChosen();
            update_freq=5;
            showChosen();
             reschedule();
            }
            return true;
        }
    });

    set_10.setOnMenuItemClickListener(new OnMenuItemClickListener(){
        public boolean onMenuItemClick(MenuItem item){
            if(checked==true){
            showChosen();
            update_freq=10;
            showChosen();
             reschedule();
            }
            return true;
        }
    });
    return true;

}

public boolean showChosen(){
    switch(update_freq){
    case(1):{
        set_1.setChecked(true);break; 
    }
    case(3):{
        set_3.setChecked(true);break;
    }
    case(5):{
        set_5.setChecked(true);break;
    }
    case(10):{
        set_10.setChecked(true);break;
    }
    }return true;
}

public void onClick(String n) {
    Toast.makeText(Wifi_infoActivity.this,n,
        Toast.LENGTH_SHORT).show();
}

public void schedule_task(){

    Runnable task = new Runnable(){ 
        public void run(){
            wifi_refresh();
     }
    };
    handler = exet.scheduleWithFixedDelay(task,0, interval, TimeUnit.MILLISECONDS);
}

public void reschedule(){
    while(interval!=(update_freq*1000)){
        handler.cancel(true);
        interval = update_freq*1000;
        handler = exet.scheduleWithFixedDelay(new Runnable(){
            public void run(){
                wifi_refresh();
            }
        }, 0, interval, TimeUnit.MILLISECONDS);
    }
}

public void wifi_refresh(){

    wifi_info = new ArrayList<String>();
    result_size =0;
    channel = "";

    lv = (ListView)findViewById (R.id.myListView);
    wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    if(!wifi.isWifiEnabled())
        if(wifi.getWifiState()!=WifiManager.WIFI_STATE_ENABLING)
            wifi.setWifiEnabled(true);

    wifi.startScan();
    results = wifi.getScanResults();
    result_size = results.size();

    for(int j =0;j<result_size;j++){
        String mystring = new String();
        ScanResult result = results.get(j);

        switch (result.frequency) {
        case 2412: channel = "1"; break;
        case 2417: channel = "2"; break;
        case 2422: channel = "3"; break;
        case 2427: channel = "4"; break;
        case 2432: channel = "5"; break;
        case 2437: channel = "6"; break;
        case 2442: channel = "7"; break;
        case 2447: channel = "8"; break;
        case 2452: channel = "9"; break;
        case 2457: channel = "10"; break;
        case 2462: channel = "11"; break;
        case 2467: channel = "12"; break;
        case 2472: channel = "13"; break;
        case 2484: channel = "14"; break;
        }

        mystring = String.format("SSID: %s,       Level: %s,       Authentication: %s,       BSSID: %s,        Frequency:%s MHz,     Channel:%s",  result.SSID.toString(), result.level, result.capabilities.toString(),result.BSSID.toString(), result.frequency, channel);
        wifi_info.add(j, mystring);
        channel = "";
    }    

    aa = new ArrayAdapter<String>(this,layoutID, wifi_info);
    lv.setAdapter(aa);
}

}

1 个答案:

答案 0 :(得分:0)

确保调用wifirefresh方法的线程也是UI线程。因为在Android中只有一个主UI线程,而其他线程不是非UI线程。 希望这会有所帮助。