我希望隐私政策,免责声明和颜色选择器作为安装应用时的一次性活动。那么我怎样才能使用持久存储,这样当我们第一次安装和运行应用程序时,隐私策略会运行,然后点击按钮(下一步)转到免责声明活动,然后转到颜色选择器,最后转到主要活动。当第二次运行应用程序时,它会直接进入主要活动。
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
}
}
答案 0 :(得分:0)
SharedPreferences
正是您要找的,在您的第一个活动中添加这段代码
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*/
}