我是Android新手,我正在开发一个应用程序。我想运行启动画面,但在我的代码中有错误,启动画面只是打开并停留在同一页面,但它没有打开下一个活动,请任何人帮助我。我的主要目标是,#34;对于新用户,首先应使用Setpassword.java
活动打开启动画面(并且密码应存储在sharedpreference
中),但是当用户设置密码并关闭时应用程序和重新打开时,应该直接使用enterpassword.java
活动打开启动画面,即跳过setpassword
。 Java活动:
package com.example.shiva.secretbook;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.logging.Handler;
/**
* Created by shiva on 8/12/2017.
*/
public class SplashScreenActivity extends Activity {
String password;
ImageView imageViewSplash;
TextView txtAppName;
RelativeLayout relativeLayout;
Thread SplashThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// load password
SharedPreferences settings = getSharedPreferences("PREFS", 0);
password = settings.getString("password", "");
imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash);
txtAppName = (TextView) findViewById(R.id.txtAppName);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
startAnimations();
}
private void startAnimations(){
Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate);
Animation translate = AnimationUtils.loadAnimation(this,
R.anim.translate);
rotate.reset();
translate.reset();
relativeLayout.clearAnimation();
imageViewSplash.startAnimation(rotate);
txtAppName.startAnimation(translate);
SplashThread = new Thread(){
@Override
public void run() {
super.run();
int waited = 0;
while (waited < 3500) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited += 100;
}
if (password.equals("")){
// if there is no password
SplashScreenActivity.this.finish();
Intent intent = new
Intent(SplashScreenActivity.this,setpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} else {
//if there is a password
Intent intent = new
Intent(SplashScreenActivity.this,enterpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} SplashThread.start();
}
};
}}
答案 0 :(得分:1)
试试这个
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SplashEnvironment(this, this));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (password.equals("")){
// if there is no password
SplashScreenActivity.this.finish();
Intent intent = new
Intent(SplashScreenActivity.this,setpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} else {
//if there is a password
Intent intent = new
Intent(SplashScreenActivity.this,enterpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
// close this activity
}
}, 3000);// time for spalsh screen
答案 1 :(得分:1)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (password.equals(""))
{
// if there is no password
Intent intent = new Intent(SplashScreenActivity.this,setpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} else
{
//if there is a password
Intent intent = new Intent(SplashScreenActivity.this,enterpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
}
}, 3500);
答案 2 :(得分:1)
尝试以下代码
public void StartAnimation(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (password.equals("")){
// if there is no password
SplashScreenActivity.this.finish();
Intent intent = new
Intent(SplashScreenActivity.this,setpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
} else {
//if there is a password
Intent intent = new
Intent(SplashScreenActivity.this,enterpassword.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
}
}, 5000);
}
答案 3 :(得分:0)
尝试以下代码
TestBox
答案 4 :(得分:0)
你在线程创建中启动了你的线程(SplashThread.start();)。
由于这个原因你的线程无法启动。所以只需将这一行放在适当的地方即。
SplashThread = new Thread(){
}; SplashThread.start();