[英]Android-Starting new Activity in Handler/runnable is really slow
我正在制作需要可運行的android應用。 我正在從runnable開始一項新活動。 出現了新活動,並且效果很好。 問題是,當調用開始活動時,它的速度非常慢。 當我希望它是瞬時的時,要花整整5秒鍾才能開始該活動。
Boolean handlerrun=true;
Intent intent= new Intent(this,newactivity.class);
int somevalue=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameactivity);
handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
if(handlerrun){somevalue++;}
if(somevalue>500){
handlerrun=false;
startActivity(intent);
finish();
}
handler.postDelayed(this, 1);}
}
};
handler.postDelayed(r, 1);
}
該活動在somevalue大於500時開始。為了阻止處理程序增加somevalue的值,我使用了布爾處理程序運行,僅當處理程序為true時才運行該處理程序。 當somevalue大於500時,handlerrun = false,因此處理程序不會增加該值。 我嘗試使用handler.removeCallbacksandMessages()方法,但是沒有用。 Logcat不會給我任何錯誤。任何幫助將不勝感激。
您可以嘗試這樣的事情:
@Override
protected void onResume() {
super.onResume();
if(done){
return;
}
done = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(), YourActivity.class));
finish();
overridePendingTransition(0, 0);
}
}, 5000);
}
這將在大約5秒鍾后啟動YourActivity 。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.