android - ACTION_MEDIA_BUTTON 在真实设备上不起作用

标签 android

我正在编写一个 Android 应用程序来自动接听来电。 我正在使用接收器,并尝试在电话响起时发送 ACTION_MEDIA_BUTTON 事件。

在模拟器上一切正常 - 当电话响起时,它实际上会自动接听电话。

但是当我在设备本身上尝试它时,它就是不起作用 (HTC Legend)。

这是代码:

// 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");

这是 XML 文件中的接收器代码:

<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>`

有人知道为什么它不能在真实设备上运行吗?有什么不同?

最佳答案

请调用此方法。它在我的 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());
}

关于android - ACTION_MEDIA_BUTTON 在真实设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3907062/

相关文章:

android - 如何拦截所有触摸事件?

android - Google Play 服务中的 NodeAPI 如何工作?

android - scrollView 内的模拟时间选择器不显示时间选择器

java - 为什么 okhttp3 client.execute 不起作用?

android - 如何检查卡是信用卡还是借记卡

java - android.content.res.Resources$NotFoundException : String resource ID #0x1

android - API 19 中的 Intent.ACTION_GET_CONTENT 错误,但 API 23 中没有

java - 有适用于 Android 的键盘小部件吗?

java - 从数据库填充 ListView 项时获取其 ID

android - 如何在 android 中暂停和恢复录音