在我的BroadcastReceiver
课程中,我使用ISharedPreferences
来存储来自服务的字符串,并将其与之前的字符串(存储在我的首选项中)进行比较。
BroadcastReceiver.cs
[BroadcastReceiver]
[IntentFilter(new[] { "TEST" })]
public class Receiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(context);
ISharedPreferencesEditor editor = pref.Edit();
string old = pref.GetString("MYKEY", "nothing");
Log.Error("lv", "OnReceive");
string new = intent.GetStringExtra("alltotale");
editor.PutString("MYKEY", new);
editor.Commit();
Notification.Builder builder = new Notification.Builder(context);
builder.SetContentTitle("Old:" + old);
builder.SetContentText("New" + new);
builder.SetSmallIcon(Resource.Drawable.Icon);
Notification notif = builder.Build();
NotificationManager notifmanager = context.GetSystemService(Context.NotificationService) as NotificationManager;
notifmanager.Notify(12, notif);
}
}
现在奇怪的是,通知中显示的两个字符串(旧的和新的)是相同的,尽管我很确定它们不是。这表明存储过程出现了问题。我不知道为什么它会在通知中给出相同的字符串,我没有看到逻辑上的任何问题,那么是什么导致了这种情况发生呢?