Android - 广播中的共享偏好

时间:2018-06-08 15:09:24

标签: c# android xamarin android-intent service

在我的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);
     }
 }

现在奇怪的是,通知中显示的两个字符串(旧的和新的)是相同的,尽管我很确定它们不是。这表明存储过程出现了问题。我不知道为什么它会在通知中给出相同的字符串,我没有看到逻辑上的任何问题,那么是什么导致了这种情况发生呢?

0 个答案:

没有答案
相关问题