ACTION_MEDIA_BUTTON无法在真实设备上运行

时间:2010-10-11 14:22:40

标签: 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>`

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

1 个答案:

答案 0 :(得分:1)

请拨打此方法。它正在我的htc野火中工作。

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());
}