Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現判斷手機未接來電及處理方法

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

編輯:關於Android編程

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved