国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)判斷手機(jī)未接來電及處理方法

2020-04-11 11:47:34
字體:
供稿:網(wǎng)友

通常來說Android手機(jī)沒有未接來電的監(jiān)聽器,如果要實(shí)現(xiàn)對未接來電的處理,則需要自己編寫程序來實(shí)現(xiàn)。本文所述程序?qū)嵗礊锳ndroid實(shí)現(xiàn)判斷手機(jī)未接來電及處理方法。主要分為四個(gè)步驟來進(jìn)行:

1、編寫CallListener,處理手機(jī)狀態(tài)變更監(jiān)聽,當(dāng)狀態(tài)改變時(shí)進(jìn)行處理:

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; //最后的狀態(tài)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;// 如果當(dāng)前狀態(tài)為空閑,上次狀態(tài)為響鈴中的話,則認(rèn)為是未接來電if(lastetState == TelephonyManager.CALL_STATE_RINGING&& state == TelephonyManager.CALL_STATE_IDLE){sendSmgWhenMissedCall(incomingNumber);}//最后改變當(dāng)前值lastetState = state;}private void sendSmgWhenMissedCall(String incomingNumber) {//未接來電處理(發(fā)短信,發(fā)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節(jié)點(diǎn)下注冊電話狀態(tài)改變的廣播接收:

<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中添加讀取手機(jī)狀態(tài)的權(quán)限:

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

通過以上的步驟,當(dāng)手機(jī)有未接來電時(shí) sendSmgWhenMissedCall 該方法就會觸發(fā),就可以進(jìn)行相應(yīng)的處理。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石嘴山市| 安溪县| 抚顺市| 尖扎县| 山西省| 凉山| 赞皇县| 波密县| 喀喇沁旗| 永登县| 方正县| 航空| 盐源县| 定安县| 钟祥市| 衡东县| 余姚市| 黎平县| 收藏| 和林格尔县| 西乌珠穆沁旗| 烟台市| 金堂县| 施甸县| 拉萨市| 安平县| 屯留县| 迁西县| 长沙县| 莱阳市| 深泽县| 玛沁县| 平舆县| 道孚县| 邹平县| 永川市| 化隆| 凤城市| 十堰市| 天台县| 稷山县|