活动不执行任务

时间:2012-08-10 22:23:35

标签: android android-asynctask

我做了这个活动,任务没有开始。我调试了它,onStart()转到task.execute()行,但它从不进入doInBackground()。在运行任务列表中有一个AsyncTask我假设它。我只是看不出问题出在哪里。这是代码。

package pl.edu.elka.pw.hosewski.wifi;

public class NetDetail extends Activity implements OnClickListener{

private LineGraph lgNet;
private WifiManager wifi;
private WiFiDetail net;
private DBSource dbs;
private DBWifi dbw;
private TextView ssid;
private TextView bssid;
private TextView freq;
private TextView capab;
private SharedPreferences getPrefs;
private int interval;
private LineRefresh task;
private ViewSwitcher psSwitch;
private Button bSwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.netdetail);
    dbs = new DBSource(this);
    dbs.open();
    getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    psSwitch = (ViewSwitcher) findViewById(R.id.psSwitch);
    bSwitch = (Button) findViewById(R.id.bSwitch);
    lgNet = (LineGraph) findViewById(R.id.lgNet);
    lgNet.setClickable(true);
    lgNet.setOnClickListener(this);
    bSwitch.setOnClickListener(this);
    Bundle b = getIntent().getExtras();
    long i = b.getLong("index");
    dbw = dbs.getWifi(i);
    net = WiFiList.getDetailByBSSID(dbw.getBssid());
    lgNet.setDetail(net);
    ssid = (TextView) findViewById(R.id.tvSSID);
    bssid = (TextView) findViewById(R.id.tvBSSID);
    freq = (TextView) findViewById(R.id.tvFrequen);
    capab = (TextView) findViewById(R.id.tvCapab);
    ssid.setText(dbw.getSsid());
    bssid.setText(dbw.getBssid());
    freq.setText(dbw.getFrequency() + " MHz");
    capab.setText(dbw.getCapability());
    this.setTitle(dbw.getSsid()+ " - detale");
    interval = Integer.parseInt(getPrefs.getString("intervals", "5"));
    task = new LineRefresh();
}


class LineRefresh extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        updLines();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        while(wifi.isWifiEnabled()){
            publishProgress();
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(isCancelled()){
                break;
            }
        }
        return null;
    }

}

public void updLines(){
    lgNet.setDetail(net);
    lgNet.postInvalidate();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    dbs.close();
    task.cancel(true);
}


@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    task.execute();
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.lgNet:
        psSwitch.showNext();
        break;
    case R.id.bSwitch:
        psSwitch.showPrevious();
        break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

task = new LineRefresh()后,添加task.execute(null)。您可能需要也可能不需要“null”参数,请参阅Eclipse告诉您的内容。