首页 > 解决方案 > Android - 广播中的共享首选项

问题描述

在我的BroadcastReceiver课程中,我ISharedPreferences用来存储来自服务的字符串并将其与前一个字符串(存储在我的首选项中)进行比较。

广播接收器.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);
     }
 }

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

标签: c#androidxamarinandroid-intentservice

解决方案


推荐阅读