获取来电号码

AndroidManifest.xml加入以下权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

 

具体实现如下

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	MyPhoneStateListener phoneListener = new MyPhoneStateListener();
	TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
	telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

public class MyPhoneStateListener extends PhoneStateListener {
	Context context;

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {
		/*
		 * state 状态
		 * incomingNumber 来电号码
		 * 
		 * TelephonyManager.CALL_STATE_RINGING
		 * TelephonyManager.CALL_STATE_IDLE
		 * TelephonyManager.CALL_STATE_OFFHOOK
		 */
		Toast.makeText(MainActivity.this, incomingNumber, Toast.LENGTH_SHORT).show();
	}
}

 

发布了1041 篇原创文章 · 获赞 5 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览