[Java] 纯文本查看 复制代码protected class MediaButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
//boolean consumed = PhoneUtils.handleHeadsetHook(phone, event);
//answerCall(phone.getRingingCall());
Method method = null;
try {
method = Class.forName("android.os.ServiceManager")
.getMethod("getService", String.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
IBinder binder = null;
try {
binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
ITelephony telephony = ITelephony.Stub.asInterface(binder);
try {
telephony.answerRingingCall();
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
}
}
}