ACTION_MEDIA_BUTTON does not work on real device
I'm writing an Android app to auto-answer incoming calls. I am using a receiver, and trying to send ACTION_MEDIA_BUTTON event when the phone rings.
It all works great on the emulator - When the phone rings it actually answers the call automatically.
But when I'm trying it on the device it-self it just does not work (HTC Legend).
This is the code:
// trigger on buttonUp instead of buttonDown
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown , "android.permission.CALL_PRIVILEGED");
// trigger on buttonUp instead of buttonDown
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
A开发者_运维技巧nd this is the receiver code in the XML file:
<receiver android:name=".PhoneOutgoingCallReceiver" android:enabled="true">
<intent-filter android:priority="0">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".PhoneIncomingCallReceiver" android:enabled="true">
<intent-filter android:priority="0">
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>`
Anyone know why it does not work on the real device? What is different?
Please call this method. It is working in my htc wildfire s.
public static void answerPhoneHeadsethook(Context context) {
try {
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown,
"android.permission.CALL_PRIVILEGED");
buttonDown = null;
} catch (Exception e) {
Toast.makeText(context, "Error First Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
try {
// froyo and beyond trigger on buttonUp instead of buttonDown
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp,
"android.permission.CALL_PRIVILEGED");
buttonUp = null;
} catch (Exception e) {
Log.e(" **** Error in Second Try: ", "" + e.getMessage());
Toast.makeText(context, "Error Second Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
try {
Intent headSetUnPluggedintent = new Intent(
Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged 1 =
// Headset with
// microphone 2 =
// Headset without
// microphone
headSetUnPluggedintent.putExtra("name", "Headset");
// TODO: Should we require a permission?
context.sendOrderedBroadcast(headSetUnPluggedintent, null);
headSetUnPluggedintent = null;
} catch (Exception e) {
Log.e(" **** Error in Third Try: ", "" + e.getMessage());
Toast.makeText(context, "Error Third Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
// Second Time call for HTC Mobile
try {
// SettingsClass.logMe(tag, "Simulating headset button");
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown,
"android.permission.CALL_PRIVILEGED");
buttonDown = null;
} catch (Exception e) {
Log.e(" **** Error in First Try: ", "" + e.getMessage());
Toast.makeText(context, "Error First Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
try {
// froyo and beyond trigger on buttonUp instead of buttonDown
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp,
"android.permission.CALL_PRIVILEGED");
buttonUp = null;
} catch (Exception e) {
Log.e(" **** Error in Second Try: ", "" + e.getMessage());
Toast.makeText(context, "Error Second Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
try {
Intent headSetUnPluggedintent = new Intent(
Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone
headSetUnPluggedintent.putExtra("name", "Headset");
// TODO: Should we require a permission?
context.sendOrderedBroadcast(headSetUnPluggedintent, null);
headSetUnPluggedintent = null;
} catch (Exception e) {
Log.e(" **** Error in Third Try: ", "" + e.getMessage());
Toast.makeText(context, "Error Third Try: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
精彩评论