Android Can';无法在应用程序启动时获取SharedPref值。安卓工作室
我的应用程序中有一个非常神秘的错误。 在Android Studio上,我正在检查第一个活动(Launcher活动)的SharedReferences中的值,如果我得到一个值,应用程序应该进入后台,否则活动将显示 问题是:当应用程序首次启动时,它将进入后台。但在第二次,它显示了活动。同样,(对于第三次发布),该应用程序将进入后台。没有调试点在工作。也没有显示任何日志。请帮我解决这个神秘的问题 谢谢Android Can';无法在应用程序启动时获取SharedPref值。安卓工作室,android,sharedpreferences,onstart,Android,Sharedpreferences,Onstart,我的应用程序中有一个非常神秘的错误。 在Android Studio上,我正在检查第一个活动(Launcher活动)的SharedReferences中的值,如果我得到一个值,应用程序应该进入后台,否则活动将显示 问题是:当应用程序首次启动时,它将进入后台。但在第二次,它显示了活动。同样,(对于第三次发布),该应用程序将进入后台。没有调试点在工作。也没有显示任何日志。请帮我解决这个神秘的问题 谢谢 Edited: public class SecureAppsMainAct extends A
Edited:
public class SecureAppsMainAct extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp_lockcode = PreferenceManager
.getDefaultSharedPreferences(this);
String set_code = sp_lockcode.getString("LOCKCODE_SET", "");
Log.e("set code", set_code + "");
if (set_code.length() > 1) {
//finish();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
} else {
Log.e("Load Prefs not working", "Load Prefs not working");
Intent i = new Intent(SecureAppsMainAct.this, Main.class);
startActivity(i);
}
}
}
清单文件:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name="com.example.checkcurrentrunningapplication.SecureAppsMainAct"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.checkcurrentrunningapplication.Main">
</activity>
<activity android:name="com.example.checkcurrentrunningapplication.LockDialog"></activity>
<activity android:name="com.example.checkcurrentrunningapplication.UnlockingAct"
android:theme="@android:style/Theme.Dialog"></activity>
<receiver android:name="com.example.checkcurrentrunningapplication.StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="StartupReceiver_Manual_Start" />
</intent-filter>
</receiver>
<receiver android:name = "com.example.checkcurrentrunningapplication.CheckRunningApplicationReceiver"/>
</application>
我重新格式化了您的代码。您应该使用布尔值而不是字符串
public class SecureAppsMainAct extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sp_lockcode = PreferenceManager
.getDefaultSharedPreferences(this);
boolean set_code = sp_lockcode.getBoolean("LOCKCODE_SET", true);
//Log.e("set code", set_code + "");
if (set_code) {
//finish();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
sp_lockcode.putBoolean("LOCKCODE_SET", false);
} else {
Log.e("Load Prefs not working", "Load Prefs not working");
Intent i = new Intent(SecureAppsMainAct.this, Main.class);
startActivity(i);
}
}}
但无论如何我会找到另一条路。我只是发布了这篇文章,这样其他人可能会觉得它很有用。请发布你的代码。你的代码可能有问题。对不起,我只是尝试了你说的方式,但没有用。SharedReferences在应用程序启动时或任何时候获取存储在其中的所有值(无论是字符串还是布尔值)。
Thank you very much all 3 who wanted to help me.
I just Found that the problem occurred after every time the code of "minimizing the app runs". So, I just commented that code. I have no idea what's the problem with this code (or on Android Life Cycle):
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);