如何在C# xamarin窗体中获取IMEI号?

pgky5nke  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(420)

I'm working with C# xamarin forms android and I'm trying to get the IMEI number. I've been searching about it and everything I found doesn't works. Now I'm trying with this code but again, Doesn't works.
Errors:

  1. The name settings does not exist in the current context.
  2. The name Forms does not exist in the current context.
    C# code:
[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace AppMobile.Models
{
    public class UniqueIdAndroid
    {
        public string GetIdentifier()
        {
            return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
        }
    }
}

Somebody knows how could I resolve this or knows other solution? Thank you very much!

UPDATE 1

c# code:

using Android.OS;
using AppMobile.Models;
using System;
using System.Collections.Generic;
using System.Text;
using static Android.Provider.Settings;
using Android.Provider;
using Xamarin.Forms;

    [assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
    namespace AppMobile.Models
    {
        public class UniqueIdAndroid : IDevice
        {
            public string GetIdentifier()
            {
                return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
            }
        }
    }

I have added the IDevice interface that I don't know really what it should be and I fixed the settings problem with using Android.Provider; . this link shows that I have to call with this:

string deviceIdentifier = DependencyService.Get<IDevice>().GetIdentifier();

But i don't understand how to implement IDevice interface.
Someone can help me? Thank you very much!

UPDATE 2

so, now I can use the function.
C# code:

using Android.OS;
using AppMobile.Models;
using System;
using System.Collections.Generic;
using System.Text;
using static Android.Provider.Settings;
using Android.Provider;
using Xamarin.Forms;
using Android.Content;

[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace AppMobile.Models
{
    public class UniqueIdAndroid
    {
        
        public string GetIdentifier()
        {
            //return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
            
            return Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Settings.Secure.AndroidId);
        }
        
    }
}

My problem is that this doesn't give me the IMEI. Insted it gives me the ID of the phone, ¿is there some way to get the IMEI string? Thank you very much!

vojdkbi0

vojdkbi01#

您可以尝试以下代码:

Android.Telephony.TelephonyManager mTelephonyMgr; 

mTelephonyMgr = (Android.Telephony.TelephonyManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.TelephonyService);

string imei = mTelephonyMgr.GetMeid(0);

从android文档getMeid中,我们知道:
从API级别29开始,永久性设备标识符受到额外限制的保护,建议应用使用可重置标识符(请参阅唯一标识符的最佳实践)。如果满足以下要求之一,则可以调用此方法:

  • 如果调用应用程序已被授予READ_PRIVILEGED_PHONE_STATE权限;这是一个特权权限,只能授予

到设备上预加载的应用程序。

  • 如果调用应用程序是完全受管设备的设备所有者、组织拥有的设备的配置文件所有者或其代理

(see())中的一个或多个。

  • 如果调用应用对任何活动订阅具有运营商权限(请参阅hasCarrierPrivileges())。
  • 如果调用应用程序是默认的SMS角色保持器
    备注:

一般来说,你无法获得READ_PRIVILEGED_PHONE_STATE权限。它只对特权系统应用可用。除非这是一个个人应用,并且你有一个根设备,这样你就可以让你的应用成为特权系统应用。
要了解更多信息,您可以查看文档getMeid。

相关问题