如何在xamarin形式的双SIM卡中检索电话号码和IMEI?



我需要从SIM卡中检索IMEI号码和电话号码。我成功地从SIM卡中获取了IMEI号码。但不检索电话号码。它在使用电话管理器线路号时显示为空。任何人都可以知道获取电话号码信息的其他一些想法吗?我正在使用上面的代码:

if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadSms) != Android.Content.PM.Permission.Granted &&
Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneNumbers) !=
Android.Content.PM.Permission.Granted && Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this,
Manifest.Permission.ReadPhoneState) != Android.Content.PM.Permission.Granted && Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != Android.Content.PM.Permission.Granted)
{
RequestPermissions(new String[] { Manifest.Permission.ReadPhoneState, Manifest.Permission.ReadPhoneNumbers, Manifest.Permission.ReadSms,Manifest.Permission.AccessCoarseLocation }, PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
else
{
setDeviceImei();
}
private void setDeviceImei()
{
var tMgr = (TelephonyManager)Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.TelephonyService);
Console.WriteLine(tMgr.Line1Number);
//string deviceId = getDeviceID(telephonyManager,0);
//string deviceId1 = getDeviceID(telephonyManager, 1);
String imeiNumber1 = tMgr.GetDeviceId(0); //(API level 23)   
String imeiNumber2 = tMgr.GetDeviceId(1);
Console.WriteLine(imeiNumber1, imeiNumber2);
SubscriptionManager smgr = (SubscriptionManager)GetSystemService(TelephonySubscriptionService);
SubscriptionInfo sim1 = smgr.GetActiveSubscriptionInfoForSimSlotIndex(0);
SubscriptionInfo sim2 = smgr.GetActiveSubscriptionInfoForSimSlotIndex(1);
Console.WriteLine(sim1);
Console.WriteLine(sim2);
}

您必须记住,Xamarin移动开发基本上是使用C#的本机开发,因此您受到与使用本机语言(iOS的Obj-C,Android的Java(构建应用程序的开发人员类似的限制。

苹果

Apple具有非常强大的隐私实现,因此它们不允许应用程序"窃取"任何可以独占设备/用户的信息。因此,无法直接从设备获取IMEI号码或电话号码。您可以在测试时使用"私有 API"来获取它,但该应用将在审核过程中被拒绝,因此没有用

但是,有一种"合法"方法可以捕获电话号码,而无需任何尚未阻止的用户数据输入,如下所述:

这个想法是让应用程序以编程方式向 具有应用唯一安装代码的服务器。然后,应用可以查询 同一服务器,以查看它最近是否收到来自 具有此唯一应用安装代码的设备。如果有,它可以 阅读发送它的电话号码。这是一个演示 显示该过程的视频。 尽你所能 看,它就像一个魅力!

人造人

您已经弄清楚了IMEI号码。对于电话号码,没有可靠的方法,尽管您可以使用与上述iOS相同的方法。以下是一些建议:

确保在你
  • 尝试过的代码中,你已经添加了权限READ_PHONE_STATE:<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  • 如果这不起作用,请尝试添加权限READ_CONTACTS&READ_PROFILE,并添加using Android.Provider;以及以下代码:
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.CommonDataKinds.Phone.Number };
var cursor = ContentResolver.Query(uri, projection, null, null, null);
var contactList = new List<string>();
if (cursor.MoveToFirst())
{
do
{
if (!cursor.MoveToNext())
break;
var phoneNumber = cursor.GetString(4); // This is the user's phone#
} while (true);
}

这有什么用例?我也许可以为您提出更好的解决方案。

最新更新