因此,我确实已经彻底寻找了我的问题的答案;通常我可以很容易地找到几乎任何事情的答案。
无论如何,基本上我设置了一个警报管理器,它最终设置了一个广播接收器。在接收器内部,它决定接收到哪个意图,删除共享首选项,然后设置启动活动的通知。问题是,在我的 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