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

所属分类: 软件编程 / Android 阅读数: 1923
收藏 0 赞 0 分享

通常来说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 该方法就会触发,就可以进行相应的处理。

更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多