Android studio将异步任务更改为处理程序

时间:2015-10-15 03:41:23

标签: android android-studio android-asynctask handler

我需要更改此应用程序,以便它使用Handler而不是AsyncTask来运行虚假下载。我已经坚持了一段时间。我已经尝试使用样本来解决它,但我认为通知线程让我感到困惑。尽我所能。

public class Responsive extends AppCompatActivity {

// 5. Declare and populate a string array for the ListView items (put this directly underneath the class declaration)
private static final String[] LIST_ITEMS = new String[]{"one", "two", "three", "four"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_responsive);

    // 4. Call setupButton() and setupListView() from the onCreate method.
    setupButton();
    setupListView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_responsive, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

// 3. Open Responsive.java for editing and add the following three methods to it (don’t get rid of any of the existing code):

private void setupListView() {
    ListView lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, LIST_ITEMS));
}

private void setupButton() {
    Button button = (Button) findViewById(R.id.notificationButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            createNotification();
        }
    });
}

private void createNotification() {
    new Downloader(getApplicationContext()).execute(0);
}
}

1 个答案:

答案 0 :(得分:-1)

foo = [['the', 100], ['at', 99], ['for', 32]]
bar = [['mitochondria', 20], ['at', 10], ['you', 9]]