0
我需要更改此應用程序,以便它使用Handler
而不是AsyncTask
來運行虛假下載。我一直在困擾它。我曾嘗試使用示例來解決它,但我認爲通知線程讓我困惑。做我的頭英寸Android Studio將異步任務更改爲處理程序
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);
}
}