微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PreferenceManager.GetDefaultSharedPreferences(Context?) 已过时:已弃用

如何解决PreferenceManager.GetDefaultSharedPreferences(Context?) 已过时:已弃用

我最近更新了我的 Android 项目中的 nugget 包,现在我在这代码中收到一条消息:

 ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);

CS0618:PreferenceManager.GetDefaultSharedPreferences(Context?) 已过时:已弃用

enter image description here

有必要改变吗?我的代码不会像这样正常工作吗?我应该完全删除 private void storetoken(String token) 吗?我不确定我是否还需要私有 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 })
                                  .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'
,

使用 using AndroidX.Preference; 而不是 using Android.Preferences;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。