c# - PreferenceManager.GetDefaultSharedPreferences(Context?) 已过时:已弃用
问题描述
我最近更新了我的 Android 项目中的 nugget 包,现在我在这行代码中收到一条消息:
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
CS0618:PreferenceManager.GetDefaultSharedPreferences(Context?) 已过时:已弃用
有必要改变什么吗?我的代码会不会像这样正常工作?我应该完全删除 private void storeToken(String token) 吗?我不确定是否还需要 private void storeToken(String token)。
using System;
using Android.App;
using Android.Content;
using Android.Util;
using Firebase.Messaging;
using System.Collections.Generic;
using Android.Preferences;
using Android.Media;
using AndroidX.Core.App;
namespace AndroidVersion
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnNewToken(string token)
{
Log.Debug(TAG, "Refreshed token: " + token);
storeToken(token);
}
private void storeToken(String token)
{
//saving the token on shared preferences
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("my_token", token);
editor.Apply();
}
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
var body = message.GetNotification().Body;
var title = message.GetNotification().Title;
Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(body, title, message.Data);
}
void SendNotification(string messageBody, string Title, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(Activity1));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this,
Activity1.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, Activity1.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(Title)
.SetContentText(messageBody)
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000, 0, 0, 0 })
.SetLights(Android.Graphics.Color.Red, 3000, 3000)
.SetPriority((int)NotificationPriority.High)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(Activity1.NOTIFICATION_ID, notificationBuilder.Build());
}
}
}
解决方案
是的,它已被弃用。使用 AndroidX 首选项库在所有设备上实现一致的行为。
您可以使用 PreferenceManager 的 AndroidX 支持库版本,即androidx.preference.PreferenceManager
不支持android.preference.PreferenceManager.
请记住将以下内容添加到您的 build.gradle 中。
implementation 'androidx.preference:preference:1.1.1'
推荐阅读
- javascript - 装饰器功能不起作用(意外令牌)
- php - 我无法让 imagemagick 在非线程安全的 Windows Server 2016 上的 PHP 中工作
- c++ - 如何运行使用 OOP 存储在容器中的实际对象的功能?
- shiny - 闪亮的 Websocket 空闲超时
- reactjs - 中继突变后使用查看器更新子组件
- sharepoint-api - 是否可以强制 SharePoint REST API 异常为英文?
- embedded-linux - devtool 命令行工具中的完成子命令是删除还是替换?
- gmail - 如何直接从 gmail 插件发送电子邮件?
- binary - 为什么位表示为数字 2?
- database - Black dot of Death SQL 潜在问题