所以我确实彻底地搜索了我的问题的答案;通常我可以很容易地找到几乎任何东西的答案。
无论如何,基本上我设置了一个警报管理器,最终设置了一个广播接收器。在接收方内部,它决定已接收的意图,删除共享首选项,然后设置启动活动的通知。问题是,在我使用 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();
部件不工作。活动将正确打开,但"警报时间1"仍然存在。在 2.2 和 2.3 设备上,已成功删除"警报时间1"。
叹了口气: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
使用 Context.MODE_MULTI_PROCESS
作为 getSharedPreferences(( 的第二个参数。问题在于,广播接收器在与活动不同的进程中运行,并且从Android 3.0开始,多进程访问共享首选项的默认行为已更改。这应该可以为您解决 4.x 设备上的问题。
但是,Android 2.3 中存在一个错误,导致在某些情况下无法可靠地从多个进程同时访问共享首选项。我们遇到了这个问题,并对此感到非常沮丧,因为它很难重现和解释(可能是某种时间问题(。
我最终所做的只是检查活动本身,上面写着"如果闹钟时间小于 0,请删除闹钟时间"。这是一个变通办法;不幸的是,我没有收到任何其他有效的答案。
您应该在清单中删除接收器中的android:process=":remote"属性。SharedPreferences不适用于新进程,并且:remote声明"执行进程ID是什么"。
这是一个古老的问题,但也许我的回答会对今天遇到这个问题的其他人有所帮助。我刚刚找到了一个解决方案,它对我有用。
创建一个服务并实现你想要的工作,然后从你的广播接收器调用服务,这意味着你的接收器的唯一工作是调用将处理你想要做的工作的服务。
简单的答案是:不要费心在接收器中做太多事情。相反,将意图调用踢到同一包层次结构中的另一个活动,您将能够同样轻松地完成它。