Android realizes the judgment of missed call and the handling method

  • 2020-06-03 08:16:10
  • OfStack

Android phones generally do not have a listener for missed calls, and if you want to handle missed calls, you need to write your own program to do so. The program example described in this paper is Android to realize the determination of missed calls and how to deal with them. It is mainly divided into four steps:

1. Write CallListener to handle monitoring of mobile phone status changes and deal with status changes:


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; // Final state 
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 the current state is idle , The last status is if the bell rang , He thought it was a missed call 
if(lastetState == TelephonyManager.CALL_STATE_RINGING
&& state == TelephonyManager.CALL_STATE_IDLE){
sendSmgWhenMissedCall(incomingNumber);
}
// Finally, change the current value 
lastetState = state;
}
private void sendSmgWhenMissedCall(String incomingNumber) {
// Missed call handling ( texting , hair email Etc. )
}
}

2. Prepare CallReceiver and register call broadcast receiver:


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. Register the broadcast reception of the change of telephone state under application node in ES15en. xml:


<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. Add the permission to read the status of the mobile phone in ES22en. xml:


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

Through the above steps, sendSmgWhenMissedCall method will trigger when there is a missed call on the mobile phone, and the corresponding processing can be carried out.


Related articles: