通常来说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" />
通过以上的步骤,当手机有未接来电时 sendSmgWhenMissedCall 该方法就会触发,就可以进行相应的处理。
新闻热点
疑难解答
图片精选