Android实现判断手机未接来电及处理方法
来源:爱站网时间:2019-11-03编辑:网友分享
现在大部分手机都是Android系统手机或者是IOS系统的手机,其实Android手机没有自带监听未接来电的功能,今天这篇文章是爱站技术频道小编和大家分享的Android实现判断手机未接来电及处理方法。
现在大部分手机都是Android系统手机或者是IOS系统的手机,其实Android手机没有自带监听未接来电的功能,今天这篇文章是爱站技术频道小编和大家分享的Android实现判断手机未接来电及处理方法。
主要分为四个步骤来进行:
1、编写CallListener,处理手机状态变更监听,当状态改变时进行处理:
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; 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,注册来电广播接收器:
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; 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节点下注册电话状态改变的广播接收:
<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中添加读取手机状态的权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
通过上面的介绍,大家对这方面知识都有所了解了吧!感兴趣的朋友不妨去操作试试看,希望大家能继续支持爱站技术频道。