-1

我的应用程序中出现了一个非常神秘的错误。在 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>
4

2 回答 2

0

我重新格式化了你的代码。您应该使用Boolean而不是String

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);
    } 
}} 
于 2016-02-12T06:53:07.737 回答
0
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);

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

于 2016-02-12T11:01:06.987 回答