如何延迟onClick(Android)
对不起,我是android java的新手。请阅读下面代码上的注释标记“/”,当我点击我的按钮时,发送邮件和卸载同时工作,如何延迟如何延迟onClick(Android),android,Android,对不起,我是android java的新手。请阅读下面代码上的注释标记“/”,当我点击我的按钮时,发送邮件和卸载同时工作,如何延迟 @Override public void onClick(View v) { // execute send mail first sendEmail(); // delayed 30 second then execute this uninstall. Uri packageUri = U
@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);
}
你可以做:
@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);
}
但是休眠线程不是一个好的开发实现,相反,您可以重构一些模块并等待一些回调或通知。您可以:
@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);
}
但是休眠线程不是一个好的开发实现,相反,您可以重构一些模块并等待一些回调或通知。您可以使用处理程序将方法延迟所需的时间。以下是如何:
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.
您可以使用处理程序将方法延迟所需的时间。以下是如何:
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.
使用postdayed():
使用postdayed():
谢谢,喜欢这个方法男士:v谢谢,喜欢这个方法男士:v这里的问题不是延迟本身,而是延迟发生在主UI线程上>锁定主UI线程不是好的做法,因为它会让用户觉得应用程序挂起。这里的问题不是延迟本身,但是延迟发生在主UI线程上>锁定主UI线程不是好的做法,因为用户会觉得应用程序挂起。