Android开发学习:[37]android 获得手机号码
1、首先我们第一步接着前面的项目,在里面新建一个activity然后再把它设为启动项。
![Android开发学习:[37]android 获得手机号码](https://exp-picture.cdn.bcebos.com/16a84fe10ef858568a76429753e9ccd2ba66cde2.jpg)
2、然后我们在界面上布局一个按钮用来作为触发器,然后一个TextView作为显示。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.basillee.demo2.MainActivity4">
<TextView android:text=""
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/textView_show_phone_number"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="显示电话号码"
android:id="@+id/button_show_phone_number"/>
</LinearLayout>
![Android开发学习:[37]android 获得手机号码](https://exp-picture.cdn.bcebos.com/a151a233ec3834bb76a448ec8714c27bd3823de3.jpg)
3、然后我们在后台代码里面的触发事件里写入TelephoneManage对象等步骤。
package com.basillee.demo2;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity4 extends ActionBarActivity {
private Button button=null;
private TextView textView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity4);
button= (Button) findViewById(R.id.button_show_phone_number);
textView= (TextView) findViewById(R.id.textView_show_phone_number);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TelephonyManager telephonyManager= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textView.setText(telephonyManager.getLine1Number());
}
});
}
}
4、然后我们点击上面的运行按钮,在运行此项目。
![Android开发学习:[37]android 获得手机号码](https://exp-picture.cdn.bcebos.com/d4071b96b814f4d03c482261cdfe474ec38323e3.jpg)
5、下面我们选择一个模拟器来运行此项目。
![Android开发学习:[37]android 获得手机号码](https://exp-picture.cdn.bcebos.com/92dd32f7dfb2dc195467b1a895def4dca13910e3.jpg)
6、然后最后我们点击按钮就可以得到模拟器上面的虚拟号码了,
![Android开发学习:[37]android 获得手机号码](https://exp-picture.cdn.bcebos.com/4759c1dae43b3b86b56aa5e4185653bbf92075e3.jpg)