Java/Android 共享首选项 - 未指导正确 activity
Java/Android Shared Preferences - Not directing to correct activity
当我的应用程序第一次启动时,我想将用户定向到字符串配置类型。我正在使用共享首选项来这样做。当用户第一次运行应用程序时,共享首选项文件应该是空的。它首先检查是否有与键 "configured" 关联的布尔值。如果没有,它应该重定向到 activity 调用的首选项。然而,它似乎只是 'skip' 超过了这个;而不是指向午餐或早餐 activity。我什至尝试清除共享首选项但无济于事。知道问题可能是什么吗?
launch_activity
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class launch_activity extends AppCompatActivity {
// private PendingIntent pending;
/*public void startAlarm() {
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
GregorianCalendar greg = new GregorianCalendar();
Calendar now = Calendar.getInstance();
greg.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 10, 30);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, greg.getTimeInMillis(), 1000 * 60 * 60 * 24, pending);
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_activity);
// set alarm
// Intent alarmIntent = new Intent(this, alarmReceiver.class);
// pending = PendingIntent.getBroadcast(launch_activity.this, 0, alarmIntent, 0);
// startAlarm();
}
protected void onStart() {
super.onStart();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
if(!(preferences.getBoolean("configured", false))) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
}
首选项activity
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.List;
public class preferences extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.configure_icon:
Intent i = new Intent(this, preferences.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private View.OnClickListener preferencesListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.back_button:
intent = new Intent(getBaseContext(), launch_activity.class);
startActivity(intent);
break;
case R.id.logoutButton:
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("loggedIn", false);
editor.commit();
intent = new Intent(preferences.this, launch_activity.class);
startActivity(intent);
break;
/* case R.id.changeButton:
intent = new Intent(preferences.this, login.class);
startActivity(intent);
break; */
}
}
;
};
@Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
Button back = (Button) findViewById(R.id.back_button);
// Button change = (Button)findViewById(R.id.changeButton);
Button logout = (Button) findViewById(R.id.logoutButton);
back.setOnClickListener(preferencesListener);
// change.setOnClickListener(preferencesListener);
logout.setOnClickListener(preferencesListener);
}
public void onStart() {
super.onStart();
Intent i = getIntent();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (i.getBooleanExtra("setUp", false)) { // if app is starting for the first time set
editor.putBoolean("configured", true);
editor.commit();
} else {
if (!(preferences.getBoolean("loggedIn", false))) { // user is not logged in, redirect
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
}
}
public void onRadioButtonClicked(View view) {
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
boolean isChecked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_boards:
if (isChecked) {
editor.putInt("Location", 2);
editor.commit();
}
break;
case R.id.radio_speciality:
if (isChecked) {
editor.putInt("Location", 3);
editor.commit();
}
}
}
}
非常感谢!
你总是运行这个代码:
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
尝试添加一个 else 子句,如下所示:
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
else {
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
您似乎在 onStart 开始了 2 个活动。
只需在您的第一个 if 上添加一个 "return",如下所示:
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
return; //HERE----------------------------------
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
当我的应用程序第一次启动时,我想将用户定向到字符串配置类型。我正在使用共享首选项来这样做。当用户第一次运行应用程序时,共享首选项文件应该是空的。它首先检查是否有与键 "configured" 关联的布尔值。如果没有,它应该重定向到 activity 调用的首选项。然而,它似乎只是 'skip' 超过了这个;而不是指向午餐或早餐 activity。我什至尝试清除共享首选项但无济于事。知道问题可能是什么吗?
launch_activity
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class launch_activity extends AppCompatActivity {
// private PendingIntent pending;
/*public void startAlarm() {
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
GregorianCalendar greg = new GregorianCalendar();
Calendar now = Calendar.getInstance();
greg.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 10, 30);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, greg.getTimeInMillis(), 1000 * 60 * 60 * 24, pending);
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_activity);
// set alarm
// Intent alarmIntent = new Intent(this, alarmReceiver.class);
// pending = PendingIntent.getBroadcast(launch_activity.this, 0, alarmIntent, 0);
// startAlarm();
}
protected void onStart() {
super.onStart();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
if(!(preferences.getBoolean("configured", false))) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
}
首选项activity
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.List;
public class preferences extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.configure_icon:
Intent i = new Intent(this, preferences.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private View.OnClickListener preferencesListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.back_button:
intent = new Intent(getBaseContext(), launch_activity.class);
startActivity(intent);
break;
case R.id.logoutButton:
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("loggedIn", false);
editor.commit();
intent = new Intent(preferences.this, launch_activity.class);
startActivity(intent);
break;
/* case R.id.changeButton:
intent = new Intent(preferences.this, login.class);
startActivity(intent);
break; */
}
}
;
};
@Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
Button back = (Button) findViewById(R.id.back_button);
// Button change = (Button)findViewById(R.id.changeButton);
Button logout = (Button) findViewById(R.id.logoutButton);
back.setOnClickListener(preferencesListener);
// change.setOnClickListener(preferencesListener);
logout.setOnClickListener(preferencesListener);
}
public void onStart() {
super.onStart();
Intent i = getIntent();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (i.getBooleanExtra("setUp", false)) { // if app is starting for the first time set
editor.putBoolean("configured", true);
editor.commit();
} else {
if (!(preferences.getBoolean("loggedIn", false))) { // user is not logged in, redirect
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
}
}
public void onRadioButtonClicked(View view) {
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
boolean isChecked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_boards:
if (isChecked) {
editor.putInt("Location", 2);
editor.commit();
}
break;
case R.id.radio_speciality:
if (isChecked) {
editor.putInt("Location", 3);
editor.commit();
}
}
}
}
非常感谢!
你总是运行这个代码:
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
尝试添加一个 else 子句,如下所示:
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
}
else {
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}
}
您似乎在 onStart 开始了 2 个活动。
只需在您的第一个 if 上添加一个 "return",如下所示:
if((preferences.getBoolean("configured", false)) == false) { // app has not yet been set-up
Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
intent.putExtra("setUp", true);
startActivity(intent);
return; //HERE----------------------------------
}
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > 4 && (cal.get(Calendar.HOUR_OF_DAY) <= 10 && cal.get(Calendar.MINUTE) < 28))
{
Intent intent = new Intent(this, breakfastHome.class);
startActivity(intent);
} else {
Intent intent = new Intent(this, lunchHome.class);
startActivity(intent);
}