【问题标题】:Broadcast receiver not affecting shared preferences in Android 4.0 (probably 3.1+)广播接收器不影响 Android 4.0(可能是 3.1+)中的共享首选项
【发布时间】:2012-05-05 19:36:46
【问题描述】:

所以我确实彻底寻找了我的问题的答案;通常我可以很容易地找到几乎任何事情的答案。

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

我最终找到了 Android 3.1 的文档和 FLAG_INCLUDE_STOPPED_PACKAGES 实现。我试着把它扔到意图上,以防万一,但它仍然不起作用。无论哪种方式,问题都不是活动的启动,而是共享偏好的简单删除。

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

这是意图开始的地方:

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

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

叹息 :D

感谢您的帮助!!

哦,如果需要的话,这是我的接收者清单:

<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

【问题讨论】:

  • 如你所见,我之前没有问过很多问题;)我希望这是第一个有价值的问题!
  • 你有没有逐步检查你的代码,看看你是否在你的 if 块内制作?
  • 是的,通知运行良好,我可以从创建的通知运行活动;唯一在我的 4.0 手机上不起作用的是共享首选项“AlertTime1”被删除。
  • 你能在第一段代码中显示你在哪里设置alertSettings吗?
  • 确保您有一个活动,并且在尝试对广播执行任何操作之前手动启动该活动。 commonsware.com/blog/2011/07/13/…

标签: android broadcastreceiver sharedpreferences


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我最终做的只是检查活动本身,上面写着“如果闹钟时间小于 0,则删除闹钟时间”。这是一种解决方法。不幸的是,我没有收到任何其他有效的答案。

    【讨论】:

      【解决方案3】:

      您应该删除清单中接收器中的 android:process=":remote" 属性。 SharedPreferences 不适用于新进程,并且 :remote 声明“无论进程 id 是什么”。

      【讨论】:

        【解决方案4】:

        我知道这是一个老问题,但也许我的回答会对当今面临这个问题的其他人有所帮助。 我刚刚找到了一个解决方案,它对我有用

        创建一个服务并在其上实现你想要的工作,然后从你的广播接收器调用服务,这意味着你的接收器唯一的工作就是调用服务来处理你想做的工作。

        【讨论】:

          【解决方案5】:

          简单的答案是:不要费心在接收器中做太多事情。相反,在同一包层次结构中对另一个 Activity 发起 Intent 调用,您将能够同样轻松地做到这一点。

          【讨论】:

          • 我会试试这个。我读过一些关于它的东西,但它似乎更多地谈论接收器中的单独线程。它在早期版本的 Android 上运行良好似乎很奇怪。
          猜你喜欢
          • 2012-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多