0

我有一个应用程序显示一个启动屏幕,然后是一个“Showonce”屏幕,我使用了应用程序首选项和一个布尔值“user_accept”来决定屏幕是否显示,无论 boolean = 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();
        }

    }

}
4

1 回答 1

1

您正在保存user_accept并检查user_accepted

请使用 public static final String 来避免此类问题:

public static final String USER_ACCEPTED = "accepted";
.........
prefEditor.putBoolean(USER_ACCEPTED , true);
.........
 settings.getBoolean(USER_ACCEPTED , false);
于 2013-02-02T16:19:59.927 回答