我有获取安卓手机来电号码的代码。但是当来电来自特定号码时,我想自动接听电话。
我在网上找到了这段代码:
public class ServiceReceiver extends BroadcastReceiver {
private static final String TAG = null;
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr = bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: " + phoneNr);
String numb = "+4348873541";
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
if (phoneNr.equals(numb)) {
ITelephony telephonyService = (ITelephony) m.invoke(telephony);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
telephonyService.answerRingingCall();
}
}
}
你能告诉我如何自动接听来自特定号码的电话吗?