gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:49:32 26 4
gpt4 key购买 nike

我正在编写一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com