服务器之家

服务器之家 > 正文

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

时间:2021-03-05 15:39     来源/作者:Android开发网

通常来说Android手机没有未接来电的监听器,如果要实现对未接来电的处理,则需要自己编写程序来实现。本文所述程序实例即为Android实现判断手机未接来电及处理方法。主要分为四个步骤来进行:

1、编写CallListener,处理手机状态变更监听,当状态改变时进行处理:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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,注册来电广播接收器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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节点下注册电话状态改变的广播接收:

?
1
2
3
4
5
6
7
8
9
<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中添加读取手机状态的权限:

?
1
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

通过以上的步骤,当手机有未接来电时 sendSmgWhenMissedCall 该方法就会触发,就可以进行相应的处理。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部