欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-02-02 13:03:28
通常来说android手机没有未接来电的监听器,如果要实现对未接来电的处理,则需要自己编写程序来实现。本文所述程序实例即为android实现判断手机未接来电及处理方法。主要...

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