c# - 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);
}
}
现在奇怪的是,通知中显示的两个字符串(旧的和新的)是相同的,尽管我很确定它们不是。这表明存储过程出现了问题。我不知道为什么它在通知中给出相同的字符串,我没有看到逻辑有任何问题,那么是什么导致了这种情况发生?
解决方案
推荐阅读
- javascript - React 路由器 v4 服务静态文件 (robot.txt)
- spring-boot - 如何在 Spring Boot 中创建/配置我自己的 AMQP ContainerFactory?
- python - DataFrame 中 read_excel 中的多个索引(TypeError:列表索引必须是整数,而不是 str)
- hibernate - 如何使用 JOIN 转换 HQL 本机查询的结果
- html - 在没有 Flash 的 html 中添加 youtube 视频
- php - 在php中解析坐标数据并避免致命的内存错误
- javascript - 30 秒后重新加载多个 iframe
- c# - 如何提供一个接口来检索和更新键、值?
- bixby - Bixby NL 和具有枚举类型的对话驱动程序
- java - java.lang.NoClassDefFoundError:无法初始化类 com.google.gdata.client.contacts.ContactsService