得到" DeviceId"并在textview中显示它

时间:2016-09-11 09:58:35

标签: android

我是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

我已经检查了this页面,我知道顶部代码的返回是"字符串" ...那么问题是什么?

1 个答案:

答案 0 :(得分:2)

aa是String类的对象。它没有名为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);
相关问题