Android實現判斷手機未接來電及處理方法_Android

來源:互聯網
上載者:User

通常來說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 該方法就會觸發,就可以進行相應的處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.