Android activity 安装应用程序时的1次运行活动

Android activity 安装应用程序时的1次运行活动,android-activity,Android Activity,我希望隐私政策、免责声明和颜色选择器在安装应用程序时作为一次性运行活动。因此,我如何使用持久性存储,以便在我们首次安装并运行应用程序时,隐私策略运行,然后单击按钮(下一步)转到免责声明活动,然后转到颜色选择器,最后转到主活动。当第二次运行应用程序时,它会直接进入主活动 MAIN ACTIVITY: public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't change this

我希望隐私政策、免责声明和颜色选择器在安装应用程序时作为一次性运行活动。因此,我如何使用持久性存储,以便在我们首次安装并运行应用程序时,隐私策略运行,然后单击按钮(下一步)转到免责声明活动,然后转到颜色选择器,最后转到主活动。当第二次运行应用程序时,它会直接进入主活动

 MAIN ACTIVITY:

public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't   change this after it's saved something

//public static final String PREFS_NAME = "MyPrefsFile"; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
      boolean firstRun = settings.getBoolean("firstRun", false); // Is it first run? If not specified, use "true"

        if (firstRun == false) {
            Log.w("activity", "first time");
            setContentView(R.layout.activity_gasfurnancediagnostics);

            SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
            editor.putBoolean("firstRun", false); // It is no longer the first run
            editor.apply(); // Save all changed settings
            return;
        }  else {
            Log.w("activity", "second time");
            checkColor();
            setContentView(R.layout.activity_theme);
            return;
        } 
       // setContentView(R.layout.activity_main);


        //*** CHECKING COLOR IF IT WAS SET PREVIOUSLY ***/

}





    public void checkColor()
    {
        // INITIALIZING THE SHARED PREFRENECES
        SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

        // CHECK IF WE HAVE COLOR_SET VALIRABLE IF NOT DEFAULT WILL BE 0
        int color_set = pref.getInt("color_set", 0);

        if(color_set==0){
              Intent e = new Intent(getApplicationContext(), ThemeActivity.class);
                startActivity(e);
                return;


        }
        if(color_set==1)
        { // IF IT IS ALREADY SET IN THE PREVIOUS THAN THIS CAN BE USED FOR REDIRECTING TO OTHER CONTROLLER AND THE BELOW FUNCTIONS CAN BE USED IN THAT CONTROLLER FOR COLOR MODIFICATIUON

            String color = pref.getString("color", null);  // COLOR CODE

            ActionBar mActionBar = getActionBar(); // GETTING ACTIONBAR
            //final Button testbutn = (Button) findViewById(R.id.testbtn);  // GETTING BUTTON

            if (color.equals("red")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFFBA1E1E));  // CHANGES ACTION BAR COLOR
               // testbutn.setBackgroundColor(0xFFFF6666); 
                // CHANGES BUTTON COLOR

            } else if (color.equals("blue")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFF21a4dd));  // CHANGES ACTION BAR COLOR
              //  testbutn.setBackgroundColor(0xFFFF4B7E); // CHANGES BUTTON COLOR

            }

            else if (color.equals("yellow")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFFF6D72B));  // CHANGES ACTION BAR COLOR
              //  testbutn.setBackgroundColor(0xFFFF4B7E); // CHANGES BUTTON COLOR

            }






                mActionBar.setDisplayShowTitleEnabled(false); // THESE TWO STEPS ARE REQUIRED AFTER CHANGING THE ACTION BAR COLOR
                mActionBar.setDisplayShowTitleEnabled(true); // THESE TWO STEPS ARE REQUIRED AFTER CHANGING THE ACTION BAR COLOR

     }









    GAS FURNANCE ACTIVITY

       Button btnnext = (Button)findViewById(R.id.nextbtn);      
       btnnext.setOnClickListener((OnClickListener) this); 
       }

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(parent.getContext(), "Button Clicked"+ dataModel.getName(),Toast.LENGTH_LONG).show();
            //Intent yes= new Intent(parent.getContext(), yes(dataModel.getName().class));
            switch(v.getId())
            {
            case R.id.nextbtn:

                 Intent a = new Intent(getApplicationContext(), MainActivity.class);
                 startActivity(a);
                 break;            


                default:


            }

        }











 package com.example.gasfurnancediagnostics;  


import android.app.Activity;  
import android.content.Intent;
import android.content.SharedPreferences; 
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;

public class ThemeActivity extends Activity implements  ColorPicker.OnColorChangedListener {  
 /** Called when the activity is first created. */  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.activity_theme);  
 Button blueBtn = (Button) findViewById(R.id.bluebtn);

blueBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors",  0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "blue");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);

      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);


  }
 });

 Button redBtn = (Button) findViewById(R.id.redbtn);

 redBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "red");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);

      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);


  }
 });

 Button yellowBtn = (Button) findViewById(R.id.yellowbtn);

yellowBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "yellow");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);


      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);



  }
 });

}







@Override
public void colorChanged(String key, int color) {
// TODO Auto-generated method stub

}   

}

SharedReferences
是您需要的,请在您的第一个活动中添加这段代码

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean firstRun = preferences.getString("FirstRun",true);

  if(true)
  {
    /*Open your activity here which you want to open for only once*/
     SharedPreferences.Editor editor = preferences.edit();
     editor.putBoolean("FirstRun",false);
     editor.apply();
  }
  else
  {
   /*Open your activity which you want to open later,generally*/
   }

我也这样做了,但按钮没有正常工作,我无法继续进行另一个活动请编辑您的问题,并在问题中发布代码(您所做的),以及错误(logcat)