java翻译到mono C#实现系列(3) 获取手机设备信息(残缺,)
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using Android.Telephony;//需要引用这个命名空间 namespace GetPhoneInfodemo { [Activity(Label = "GetPhoneInfodemo", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it TelephonyManager telphoneMgr = (TelephonyManager)GetSystemService(Context.TelephonyService); string Imei = telphoneMgr.DeviceId;//IMEI号.需要READ_PHONE_STATE权限 string PhoneType = telphoneMgr.PhoneType.ToString();//手机网络制式,例如GSM string str0 = telphoneMgr.NetworkType.ToString();//网络模式 "Hspa" string PhoneNum = telphoneMgr.Line1Number;//手机号 string SdkVersion = telphoneMgr.DeviceSoftwareVersion;//IMEI SV string ReleaseVersion = telphoneMgr.NeighboringCellInfo.ToString();//未知,安卓是获取周围基站的信息 getNeighboringCellInfo //需要ACCESS_COARSE_LOCATION权限 string str1 = telphoneMgr.CallState.ToString();//电话状态:CALL_STATE_IDLE=0无活动,CALL_STATE_RINGING=1响铃24,CALL_STATE_OFFHOOK=2摘机 CellLocation str2 = telphoneMgr.CellLocation;//电话定位 DataActivity str3 = telphoneMgr.DataActivity; TextView txtimei = FindViewById<TextView>(Resource.Id.imei); txtimei.Text = Imei; //button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; } } }
调试android程序出现异常:
java.lang.SecurityException:requires READ_PHONE_STATE
根据异常提示,发现是手机的设备号获取失败
异常原因:AndroidMinifest.xml中没有添加READ_PHONE_STATE权限
解决法案:修改AndroidMinifest.xml,在<application/>之后添加权限设置
Androidmainfest.xml代码
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
总结:Security 英文翻译“安全,安全的”, 顾名思义SecurityException就是指安全异常,容易联想到是权限限
制导致的异常,手机软件对权限的限制是很多的,后面requires READ_PHONE_STATE一目了然就是要求READ_PHONE_STATE
这个权限
另外在虚拟机如果获取不到部分信息,请到真实机子调试.