スプラッシュ画面を表示してから「Showonce」画面を表示するアプリケーションがあります。アプリの設定とブール値の「user_accept」を使用して、画面が表示されるかどうかを決定しました。ブール値が true であるかどうかに関係なく、まだページが表示されます、ページの私のコードを参照してください。どんな助けでも大歓迎です:)。
package com.overclockerz.webtest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
import android.widget.Toast;
public class SplashScreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_layout);
final SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
final boolean hasAgreed = settings.getBoolean("user_accepted", false);
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (hasAgreed == false){
Intent intent = new Intent(SplashScreen.this, ShowOnce.class);
SplashScreen.this.startActivity(intent);
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS NOT ACCEPTED",Toast.LENGTH_LONG).show();
}else if (hasAgreed == true){
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS ACCEPTED",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
finish();
}
}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
}
package com.overclockerz.webtest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class ShowOnce extends Activity implements OnClickListener {
Button show_options_dialog, accept_button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_once);
show_options_dialog = (Button) findViewById(R.id.show_options_dialog);
accept_button = (Button) findViewById(R.id.accept_button);
show_options_dialog.setOnClickListener(this);
accept_button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == show_options_dialog){
Intent intent = new Intent(getApplicationContext(), SettingScreen.class);
startActivity(intent);
}
else if (v == accept_button){
SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putBoolean("user_accept", true);
Toast.makeText(getApplicationContext(), "DEBUG: USER CLICKED ACCEPT", Toast.LENGTH_LONG).show();
prefEditor.commit();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}
}