首页 > 系统 > Android > 正文

Android实现判断手机未接来电及处理方法

2020-02-21 17:25:42
字体:
来源:转载
供稿:网友

现在大部分手机都是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" />

通过上面的介绍,大家对这方面知识都有所了解了吧!感兴趣的朋友不妨去操作试试看,希望大家能继续支持武林技术频道。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表