Android有未接来电后处理(判断未接来电)

  在Android的手机状态中没有未接来电的监听器,所以如果想当手机有未接来电后进行处理,这时候就需要自己对手机的状态判断是未接来电后再进行处理.

实现思路 :
1,继承PhoneStateListener后,当手机的状态改变后将会触发onCallStateChanged.手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音).
2,记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.

不足:
1,我现在的处理不能判断出是用户是否主动不接电话.

实现步骤:
1,编写CallListener,处理手机状态变更监听,当状态改变时进行处理。如果想知道如何在Android发送短信可以看我另一博文[ Android中发送短信(sms) ]

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
37
38
39
40
41
42
43
44
45
46
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;
 
/**
 * @author www.r-base.net
 */
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
24
25
26
27
28
29
30
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;
 
/**
 * @author www.r-base.net
 */
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" />

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值