获取来电号码实例

MainActivity.java代码如下:

package com.chen.phonecallstate;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends Activity {
	TelephonyManager manager;
	TextView textView;
	String result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取电话服务
		manager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
		//手动注册对PhoneStateListener中的listen_call_state状态进行监听
		manager.listen(new MyPhoneStateListner(), PhoneStateListener.LISTEN_CALL_STATE);
		
		textView = (TextView)findViewById(R.id.textView1);
		
	}
	
	//定义一个PhoneStateListener类.
	class MyPhoneStateListner extends PhoneStateListener {
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			super.onCallStateChanged(state, incomingNumber);
			switch (state) {
				case TelephonyManager.CALL_STATE_RINGING:
					result = incomingNumber;
					break;
				default:
					break;
			}
			textView.setText(result);
		}	
	}
	
}


 

发布了112 篇原创文章 · 获赞 15 · 访问量 28万+
展开阅读全文

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

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

分享到微信朋友圈

×

扫一扫,手机浏览