对不起我是关于android java的新手。请阅读评论标签" //"在下面的代码中,当我点击我的按钮时,发送邮件和卸载是同时工作,如何延迟呢?
@Override
public void onClick(View v) {
// execute send mail first
sendEmail();
// delayed 30 second then execute this uninstall.
Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart");
Intent uninstallIntent =
new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
}
答案 0 :(得分:0)
你可以这样做:
@Override
public void onClick(View v) {
// execute send mail first
sendEmail();
Thread.sleep(30000L);// delayed 30 second then execute this uninstall.
Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart");
Intent uninstallIntent =
new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
}
但它不是一个很好的开发实现来睡眠线程,而是你可以重构一些模块并等待一些回调或通知。
答案 1 :(得分:0)
您可以使用处理程序将方法延迟所需的时间。方法如下:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Your method to be executed after the delay.
}
}, 1000); //1000 is the time in milliseconds( 1 sec) to wait.
答案 2 :(得分:0)
使用postDelayed():
@Override public void onClick(View v) {
// execute send mail first
sendEmail();
v.postDelayed(new Runnable() {
@Override
public void run() {
Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart");
Intent uninstallIntent =
new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
}
}, 30000L);
}