使用通知意图时不保存SharedPreferences

时间:2014-12-07 20:08:45

标签: android sharedpreferences android-notifications

我的活动中有以下代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String databaseVersionKey = "databaseVersion";
    int databaseVersion = MyStore.DATABASE_VERSION;

    // check if the database needs to be upgraded
    if (MyStore.DATABASE_VERSION != sharedPreferences.getInt(databaseVersionKey, 0)) {
        Editor e = sharedPreferences.edit();
        e.putInt(databaseVersionKey, databaseVersion);
        boolean result = e.commit(); // result is true

        ...
    }

...

}

我调试并且第一次调用我的活动时检查数据库版本,然后设置是否不是最新的。

但是,当从我的通知意图调用我的活动时,未设置共享首选项中的值。 I.E.一旦我启动我的应用程序,我单击主页按钮,然后向下滑动通知栏并单击我的通知再次激活上述活动。我注意到,当我再次调试时,我的dataVersion似乎没有设置。

以下是我用于创建通知意图的代码,该通知意图启动与上述相同的活动:

    private void createNotification() {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My App")
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_MAX);

        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.setBigContentTitle("My App");

        String msg = "You are logged in, click to open app.";
        bigTextStyle.bigText(msg);
        builder.setContentText(msg);

        builder.setStyle(bigTextStyle);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MyActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MyActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(100, builder.build());
    }

我已经尝试过提交并申请,但没有任何运气。第一次从通知意图启动MyActivity时,似乎没有设置databaseVersion的值。这是在我看到它在应用程序启动时成功设置之后。

这个让我疯了,任何想法???

编辑@alfasin

getDefaultSharedPreferences android源代码:

public static SharedPreferences getDefaultSharedPreferences(Context context) {
    return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
        getDefaultSharedPreferencesMode());
}

private static String getDefaultSharedPreferencesName(Context context) {
    return context.getPackageName() + "_preferences";
}

private static int getDefaultSharedPreferencesMode() {
    return Context.MODE_PRIVATE;
}

我的包名不会因呼叫而改变,因此我每次都会获得相同的SharedPreferences。

0 个答案:

没有答案