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

NO IMAGE

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

您可能感興趣的文章:

android專案手機衛士來電顯示號碼歸屬地Android監聽來電和去電的實現方法Android6.0來電號碼與電話薄聯絡人進行匹配Android監聽手機電話狀態與傳送郵件通知來電號碼的方法(基於PhoneStateListene實現)Android中監聽未接來電的2種方法Android實現獲取未接來電和未讀簡訊數量的方法android在root模式下接聽來電的方法android實現來電靜音示例(監聽來電)android 電話狀態監聽(來電和去電)實現程式碼Android來電攔截的實現方法