【IT168 技术】最近项目要做一个能捕获到来电事件,取得电话号码的应用.在网上查了二天,算是运行成功了.在这里分享一下经验:)通过之前对Android 的学习,认为来电话时应该是通过广播(broadcast)通知给应用的.所以就在Reference里找,知道Broadcast Action应该在 anroid.content.Intent类里找.找了找Standard Broadcast Action,竟然没有...但觉得ACTION_ANSWER比较像, (Activity Action: Handle an incoming phone call.)但又是Activity Action.BroadCast的Filter应该是用不了的. 找了半天,感觉这条路比较难达到目的,就去Google了.非常郁闷的是,很多搜索结果的网站都让中国给屏闭了,很多都打不开. 不过总算也找到了一些零碎的代码, 试着去运行,但在Console里一直没有输出日志,那会以为是代码问题,解决了半天,最后才恍然大悟,日志应该在LogCat里看,才发现代码是好的. 不知道什么时候我把Logcat视图给关了,之后就忘了这事了.还要注意的是,权限一定要在Manifest文件中设置()
运行代码,成功!!在LogCat里也输出了电话号码.但看代码有很多迷惑的地方,比如说Manifest文件里intent-filter的 action:android.intent.action.PHONE_STATE这是哪来的?我在官方的Reference里没有搜到!!后面在以前的Reference里找到有,难道2.1把它不推荐使用了?在2.1的Reference里,一点它的身影都没有.
后面想看看官方自带的Phone应用在这里是怎么写的,去官方下载了Phone App应用 的源代码
git clone git://android.git.kernel.org/platform/packages/apps/Phone.git
但奇怪的是也搜不到android.intent.action.PHONE_STATE这个字符串,说明不是用的这种方式.
静下心来看了看Phone的源代码,有点失望,没看明白是怎么捕获到的.还用了很多内部的类,这些类都SDK中都没有暴露出来,去Google了一下,还有人和我一样的疑问,不知道怎样使用这些内部的类.估计官方不想我们这么去用.但应该怎么用呢?
下面这些代码在2.1上是可以运行的.但觉得不是2.1推荐的方式,因为android.intent.action.PHONE_STATE在2.1Reference里找不到....
Java代码
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CustomBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.i(TAG, "phoneNr: "+phoneNr);
}
}
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CustomBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.i(TAG, "phoneNr: "+phoneNr);
}
}
Java代码
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
@Override
public void onCallStateChanged(int state, String incomingNumber){
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "OFFHOOK");
break;
}
}
}
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
@Override
public void onCallStateChanged(int state, String incomingNumber){
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "OFFHOOK");
break;
}
}
}
Java代码
▲