Android有未接来电后处理(判断未接来电)
时间:2011年08月21日作者:么吉查看次数:487 views评论次数:1
在Android的手机状态中没有未接来电的监听器,所以如果想当手机有未接来电后进行处理,这时候就需要自己对手机的状态判断是未接来电后再进行处理.
实现思路 :
1,继承PhoneStateListener后,当手机的状态改变后将会触发onCallStateChanged.手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音).
2,记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.
不足:
1,我现在的处理不能判断出是用户是否主动不接电话.
实现步骤:
1,编写CallListener,处理手机状态变更监听,当状态改变时进行处理。如果想知道如何在Android发送短信可以看我另一博文[ Android中发送短信(sms) ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package rbase.app.smshelpmate.call.listener; import java.text.MessageFormat; import rbase.app.smshelpmate.Config; import rbase.app.smshelpmate.R; import rbase.app.smshelpmate.call.enums.CallStateEnum; import rbase.app.smshelpmate.forward.ForwardManager; import rbase.app.smshelpmate.forward.enums.ForwardType; import rbase.app.smshelpmate.forward.vo.ForwardParam; import android.content.Context; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; /** * @author www.r-base.net */ public class CallListener extends PhoneStateListener { private static final String TAG = "sms"; private static int lastetState = TelephonyManager.CALL_STATE_IDLE; // 最后的状态 private Context context; public CallListener(Context context) { super(); this.context = context; } public void onCallStateChanged(int state, String incomingNumber) { Log.v(TAG, "CallListener call state changed : " + incomingNumber); String m = null; // 如果当前状态为空闲,上次状态为响铃中的话,则破觚为认为是未接来电 if(lastetState == TelephonyManager.CALL_STATE_RINGING && state == TelephonyManager.CALL_STATE_IDLE){ sendSmgWhenMissedCall(incomingNumber); } // 最后的时候改变当前值 lastetState = state; } private void sendSmgWhenMissedCall(String incomingNumber) { // ... 进行未接来电处理(发短信,发email等等通知) } } |
2,编写CallReceiver,注册来电广播接收器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package rbase.app.smshelpmate.call.service; import rbase.app.smshelpmate.Const; import rbase.app.smshelpmate.call.listener.CallListener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; /** * @author www.r-base.net */ public class CallReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Log.i("sms", "CallReceiver Start..."); TelephonyManager telephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); CallListener customPhoneListener = new CallListener(context); telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); Bundle bundle = intent.getExtras(); String phoneNr = bundle.getString("incoming_number"); Log.i("sms", "CallReceiver Phone Number : " + phoneNr); } } |
3,在AndroidManifest.xml中的application节点下添加如下代码.进行注册电话状态改变广播接收.
1 2 3 4 5 6 7 8 9 | <manifest ...> <application ...> <receiver android:name=".call.service.CallReceiver"> <intent-filter android:priority="100"> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> </application> </manifest> |
4,在AndroidManifest.xml中添加读取手机状态的权限.
1 | <uses-permission android:name="android.permission.READ_PHONE_STATE" /> |
总结:
通过以上的步骤,当手机有未接来电时 sendSmgWhenMissedCall 该方法就会触发,就可以进行相应的处理.本人的其中一个应用(RBase短信助手) 就是用以上的代码实现了当手机有未接来电后可以发短信给指定的手机进行通知. 想了解的朋友可以到这里下载 : http://www.goapk.com/pkg/rbase.app.smshelpmate
相关博文:
Android中发送短信(sms)
Android文章汇总

最新评论
我在调出这个窗口的时候,暂停了我 »
Post:2011-11-29 19:31:02出8.0.5了,能否做一下破解呢? :mrgreen:
Post:2011-11-10 17:05:58:razz: 一直在找这个东西,甚至也 »
Post:2011-10-21 21:29:42Alarm 这个类是一个挺有意思的类, »
Post:2011-10-19 15:05:46回来看看等DbVisualizer 8.0 新版 »
Post:2011-10-10 19:20:21好东西,收藏了。
Post:2011-09-25 22:36:38:twisted: 不麻烦的话,能否能做 »
Post:2011-09-05 16:10:59你可以到hostloc去找IDC,很多。 X »
Post:2011-08-28 12:37:23