android - 广播接收器不影响 Android 4.0(可能是 3.1+)中的共享首选项

标签 android broadcastreceiver sharedpreferences

所以我确实已经彻底搜索了我的问题的答案;通常我可以很容易地找到几乎所有问题的答案。

无论如何,基本上我设置了一个警报管理器,它最终设置了一个广播接收器。在接收器内部,它决定接收到哪个 Intent ,删除共享首选项,然后设置启动 Activity 的通知。问题是在我的 4.0 手机上,共享首选项没有成功删除,但在我试过的任何以前的手机(2.2、2.3)上它都运行良好。

我最终找到了 Android 3.1 的文档和 FLAG_INCLUDE_STOPPED_PACKAGES 实现。为了以防万一,我试着把它扔到 Intent 上,但它仍然没有用。无论哪种方式,问题都不是 Activity 的启动,而是共享首选项的简单删除。

我希望这已经足够清楚了!我将在下面放入一些代码。

这是 Intent 开始的地方:

Calendar cal = Calendar.getInstance();
int seconds = 5 * 60; // 1 * 24 * 60 * 60;
cal.add(Calendar.SECOND, seconds);

Intent intent = new Intent(SetAlertActivity.this, ReminderReceiver.class);
intent.putExtra("id", "FAlert");
//intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), FRAUD_ALERT_CODE, intent, 0);

AlarmManager alertManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alertManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

settingsEditor = alertSettings.edit();
settingsEditor.putLong("AlertTime1", cal.getTimeInMillis());
settingsEditor.commit();

然后是广播接收器 onReceive():

    nContext = context;
    alertSettings = nContext.getSharedPreferences(MainActivity.PREFERENCE_FILENAME, 0);
    if (intent.getStringExtra("id").equals("FAlert"))
    {

        settingsEditor = alertSettings.edit();
        settingsEditor.remove("AlertTime1");
        settingsEditor.commit();

        String ns = Context.NOTIFICATION_SERVICE;
        int icon = R.drawable.ar_icon;
        CharSequence tickerText = nContext.getString(R.string.notification_ticker);
        CharSequence contentTitle = nContext.getString(R.string.notification_title);
        CharSequence contentText = nContext.getString(R.string.notification_text);
        long when = System.currentTimeMillis();

        NotificationManager mNotificationManager = (NotificationManager) nContext.getSystemService(ns);
        Notification notification = new Notification(icon, tickerText, when);

        Intent notificationIntent = new Intent(nContext, SetAlertActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(nContext, 135, notificationIntent, 0);

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(nContext, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

所以,正如我之前提到的,在我的 4.0 设备上(我没有任何 3.X 设备)

settingsEditor = alertSettings.edit();
settingsEditor.remove("AlertTime1");
settingsEditor.commit();

部分不工作。该 Activity 将正确打开,但“AlertTime1”仍然存在。在2.2和2.3设备上,“AlertTime1”成功删除。

感叹 :D

感谢您的帮助!

哦,如果需要的话,这是我给接收者的 list :

<receiver
    android:name="ReminderReceiver"
    android:process=":remote" >
</receiver>

这就是区别所在:

    alertSettings = getSharedPreferences(AlertRenewActivity.PREFERENCE_FILENAME, 0);
    settingsEditor = alertSettings.edit();
    if (alertSettings.contains("AlertTime1"))
    {
        alertTime = alertSettings.getLong("AlertTime1", 0);
        timeLeft = (int) ((alertTime - System.currentTimeMillis()) / (1000L));
        daysLeft = timeLeft / (60 * 60 * 24);
        daysLeftView.setText(Integer.toString(daysLeft));
        setAlert.setEnabled(false);
        setAlert.setTextColor(R.color.dark_text);
    }
    else
    {
        daysLeftView.setText(R.string.no_alert_set);
    }

在我的旧手机上,它正确地重置为“未设置警报”,但在 4.0 手机上它仍然显示“0”天剩余(这是它所说的,因为我只将警报设置为 5 分钟左右供测试用)。基本上,用户无法设置新警报,因为它没有正确重置,而且,仅在我正在尝试的 4.0 手机上:P

最佳答案

使用 Context.MODE_MULTI_PROCESS 作为 getSharedPreferences() 的第二个参数。问题是您的广播接收器在与您的 Activity 不同的进程中运行,并且从 Android 3.0 开始,多进程访问共享首选项的默认行为已经改变。这应该可以解决您在 4.x 设备上的问题。

但是,Android 2.3 中存在一个错误,导致在某些情况下无法可靠地从多个进程同时访问共享首选项。我们遇到了这个问题并对此感到非常沮丧,因为它很难重现和解释(可能是某种时间问题)。

关于android - 广播接收器不影响 Android 4.0(可能是 3.1+)中的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10465318/

相关文章:

android - com.android.vending.INSTALL_REFERRER 不工作

android - 我无法接收有关电池状态变化的广播?

android - DeadObjectException 尝试使用 Context.sendOrderedBroadcast 时

java - 我应该在 onCreate 或 onResume 中从共享首选项加载值吗?

android - 我无法从我的模块引用 SharedPreferences

java - 更改可见性导致我的应用程序崩溃,为什么?

android - 你好,世界和 Android 模拟器没有一路启动!

android - 从 fragment 列表项打开 Activity

android - Sqlite 更新查询语法错误

Android - ToggleButton setChecked 和 setSelected 不工作