[英]How to use Intent in postDelayed - Android
This is for a splash screen.这是用于启动画面。 I've followed the tutorial but it's still not working.我已按照教程进行操作,但仍然无法正常工作。 It keeps on getting error for some reason.由于某种原因,它不断出错。 I've tried everything that I know but it still doesn't work.我已经尝试了我所知道的一切,但它仍然不起作用。 Please help.请帮忙。
This is my code:这是我的代码:
package id.ac.umn.finalproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
new Handler().postDelayed(startActivity(startApp), 3000);
}
}
In Kotlin Try This在 Kotlin 试试这个
Handler(Looper.getMainLooper()).postDelayed({ Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class); startActivity(startApp) }, 3000)处理程序(Looper.getMainLooper()).postDelayed({ Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class); startActivity(startApp) }, 3000)
Try this...尝试这个...
`int SPLASH_TIME_OUT = 3000;
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
new Handler().postDelayed(() -> {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(i);
// close this activity
finish();
}, SPLASH_TIME_OUT);`
The correct way to create a Splash screen is as follows创建启动画面的正确方法如下
style.xml样式.xml
<style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>
Splash.xml飞溅.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black" /> // background color
<item
android:drawable="@drawable/ic_launcher" /> // logo
android:gravity="center" />
</layer-list>
Manifest显现
<activity
android:name=".SplashActivity"
android:exported="true"
android:noHistory="true"
android:theme="@style/SplashStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashActivity.java SplashActivity.java
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_splash); // No need setContentView
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
You're almost using postDelayed(Runnable, long)
correctly, but just not quite.您几乎可以正确使用postDelayed(Runnable, long)
,但并不完全正确。 Let's take a look at your Runnable.让我们看看你的 Runnable。
final Runnable r = new Runnable() {
public void run() {
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
handler.postDelayed(startActivity(startApp), 3000);
}
};
When we call r.run();
当我们调用r.run();
the first thing it's going to do is tell your handler
to run the very same Runnable after 3000 milliseconds
, and then to call startApp.
它要做的第一件事是告诉你的handler
在3000 milliseconds
后运行相同的 Runnable,然后调用startApp.
What this will actually result in is your startApp.
这实际上会导致您的startApp.
being called twice:
once right away, and a second time once the Handler is done waiting 3000 milliseconds.
被called twice:
一次是立即调用,第二次是在 Handler 完成后等待3000 milliseconds.
Instead, you should change your Runnable to this:相反,您应该将 Runnable 更改为:
final Runnable r = new Runnable() {
public void run() {
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
}
};
And call it like this:并这样称呼它:
handler.postDelayed(r, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.