-1

アプリで非常に不可解なバグが発生しています。Android Studio では、最初のアクティビティ (ランチャー アクティビティ)でSharedPreferencesの値をチェックしています。値を取得した場合は、アプリをバックグラウンドにする必要があります。それ以外の場合、アクティビティが表示されます。

問題は、アプリが最初に起動されたときにバックグラウンドになることです。しかし、2回目は活躍を見せています。繰り返しますが (3 回目の起動では)、アプリはバックグラウンドになります。デバッグ ポイントが機能していません。また、ログは表示されません。この不思議な問題を取り除くのを手伝ってください。

ありがとう。

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

あなたのコードを再フォーマットしました。Stringの代わりにBooleanを使用する必要があります

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 に答える