在应用启动时无法获得SharedPref值。 Android Studio

时间:2016-02-12 05:22:09

标签: android sharedpreferences onstart

我在我的应用中遇到了一个非常神秘的错误。 在Android Studio上,我在第一个Activity(Launcher Activity)上检查 SharedPreferences 中的值,如果我得到一个值,应该应该进入后台,否则将显示Activity 。

问题是:首次启动应用时,它会转到后台。但在第二次,它显示了活动。再次,(第三次发布),应用程序将进入后台。没有调试点正在运行。也没有显示日志。请帮我摆脱这个神秘的问题。

感谢。

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>

2 个答案:

答案 0 :(得分:0)

我重新格式化了你的代码。您应该使用布尔而不是字符串

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);
    } 
}} 

答案 1 :(得分:0)

<svg viewBox='0 0 900 400' preserveAspectRatio="none">
  <defs>
    <clipPath clipPathUnits="objectBoundingBox" id="clipper-left">
      <path d="M0,0 1,0 0,1z" />
    </clipPath>
    <clipPath clipPathUnits="objectBoundingBox" id="clipper-right">
      <path d="M1,0 0,1 1,1z" />
    </clipPath>
  </defs>
  <image xlink:href="http://lorempixel.com/900/400/nature/1" x="0" y="0" height="400px" width="900px" />
  <image xlink:href="http://lorempixel.com/900/400/nature/2" x="0" y="0" height="400px" width="900px" />
</svg>

但无论如何我会找到另一种方式。我刚发布这个,所以任何其他人都可能觉得它很有用。