在Android的手机状态中没有未接来电的监听器,所以如果想当手机有未接来电后进行处理,这时候就需要自己对手机的状态判断是未接来电后再进行处理.

实现思路 :
1,继承PhoneStateListener后,当手机的状态改变后将会触发onCallStateChanged.手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音).
2,记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.

不足:
1,我现在的处理不能判断出是用户是否主动不接电话.

实现步骤:
1,编写CallListener,处理手机状态变更监听,当状态改变时进行处理。如果想知道如何在Android发送短信可以看我另一博文[ Android中发送短信(sms) ]

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
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,注册来电广播接收器。

123456789101112131415161718192021222324252627282930
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节点下添加如下代码.进行注册电话状态改变广播接收.

123456789
 ...>   ...>     android:name=".call.service.CallReceiver">     android:priority="100">     android:name="android.intent.action.PHONE_STATE" />    >    >  >>

4,在AndroidManifest.xml中添加读取手机状态的权限.

1
 android:name="android.permission.READ_PHONE_STATE" />

更多相关文章

  1. Android获取手机经纬度
  2. Android 获取手机(ios,android)的设备唯一码(mac地址, IMEI)
  3. Android Activity生命周期和状态
  4. Android 判断网络状态,并且在没有网络的时候,提示网络未开启
  5. Android中程序的停止状态详细介绍
  6. 【Android】Android中不同手机分辨率适配问题
  7. android 利用adb连接手机调试程序
  8. Android开发11――手机横屏和竖屏与android:configChanges

随机推荐

  1. 判断移动终端是安卓还是iOS
  2. Android资源,国内镜像站点,博客文章等
  3. Android(安卓)WebView的使用(一)
  4. Android(安卓)Studio(七):项目从Eclipse到
  5. 如何进行Android单元测试
  6. [RK3288][Android6.0] 调试笔记 --- 替换
  7. Android深入浅出之Audio
  8. Android(安卓)APK瘦身之Android(安卓)Stu
  9. NCNN: 应用于手机上的卷积加速
  10. Android与H5互调