我是android的新手,我正在尝试获取电话序列号,并在文本视图中显示。
这是我的密码:
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mngr.getDeviceId();
String aa;
aa.setText(mngr);
TextView tt = (TextView) findViewById(R.id.phoneidtxtview);
tt.setText(aa);但我知道这个错误:
类型字符串的方法setText(TelephonyManager)未定义。
我已经检查了这页面,我知道顶部代码的返回是“string”.那么问题是什么呢?
发布于 2016-09-11 10:05:47
aa是类字符串的对象。它没有一个叫做setText()的方法。这就是你要做的:
在清单文件中声明此权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>现在以这种方式获取DeviceId():
String identifier = null;
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null)
identifier = tm.getDeviceId();
if (identifier == null || identifier .length() == 0)
identifier = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);将标识符设置为TextView tt的文本
TextView tt = (TextView) findViewById(R.id.phoneidtxtview);
tt.setText(identifier);发布于 2021-12-12 03:11:48
你必须在你的活动中实现这一点。
@SuppressLint({"MissingPermission", "HardwareIds"})或者你的清单文件上的这个权限
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>这将在您的设备上工作,但这将不会得到谷歌Playstore的批准,如果您计划将其上传到playstore,除非您的应用程序是系统应用程序,否则如果您的应用程序是普通应用程序,则不允许使用此应用程序。你的代码应该是这样的。
@SuppressLint({"MissingPermission", "HardwareIds"})
String identifier = null;
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null){
identifier = telephonyManager.getDeviceId();
}
if (identifier == null || identifier.length() == 0){
identifier = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
}
mTextView.setText(identifier);https://stackoverflow.com/questions/39434911
复制相似问题