在onCreate
中,只需调用onClick
处理函数并将您的按钮作为参数。在里面启动AsyncTask
在后台执行冗长的操作。然后只需为用户显示一个AlertDialog
。
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button launch = (Button) findViewById(R.id.launch);
launch.setOnClickListener(this);
onClick(launch); // Magic.
showDialog(); // Show the interrupt dialog
}
@Override public void onClick(View v)
{
Toast.makeText(this, "Wohoo!", Toast.LENGTH_SHORT).show();
// Actually, launch an AsyncTask here.
}
private void showDialog()
{
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Interrupt?");
dlg.setMessage("Do you want to interrupt loading?");
dlg.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int item)
{
Toast.makeText(getApplicationContext(), "Yes!!!", Toast.LENGTH_SHORT).show();
}
});
dlg.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int item)
{
Toast.makeText(getApplicationContext(), "Noooo...", Toast.LENGTH_SHORT).show();
}
});
dlg.show();
}
我认为这可以通过广播实现 – 2011-11-23 21:01:13
我不明白你想要做什么 - 你自动启动的是,用户可以选择中断吗? – Squonk