服务不是通过AsyncTask启动的

时间:2012-07-07 22:35:43

标签: android service android-asynctask

我有一个在AsyncTask完成后启动的服务。但是,当我在模拟器上管理正在运行的应用程序时,它并不存在。这是我的UI线程代码:

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.button1) {
        if (rBoot.isChecked()) {
            registerReceiver(new BootReceiver(), filter);
        }

        if (cb.isChecked())
            sent.putInt("progress", -10);
        else
            sent.putInt("progress", sb.getProgress());

        prog.show();
        new ProgressUpdate().execute(0);

    }

    if (v.getId() == R.id.button2) {
        stopService(i);
    }
}

和AsyncTask:

private class ProgressUpdate extends AsyncTask<Integer, String, String> {

    @Override
    protected String doInBackground(Integer... params) {
        // TODO Auto-generated method stub

        for (int i = 0; i <= 100; i++) {
            try {
                Thread.sleep((long) 31.4);
                progress = i;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (progress == 100) {
                prog.dismiss();
            }
        }

        return "done";
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        startService(new Intent(Activity.this, Service.class)
                .putExtra("bun", sent));
    }
}

对话框显示然后被解除,但服务永远不会启动。

0 个答案:

没有答案