当我尝试为来电创建自定义屏幕时,我正在尝试以编程方式接听来电.我使用以下代码但它在Android 5.0中不起作用.
// Simulate a press of the headset button to pick up the call
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");
// 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");
Run Code Online (Sandbox Code Playgroud) 我正在开发一个具有以下必要条件的应用程序:如果设备中插入了耳机并且用户将其删除,我需要将所有流静音.要做到这一点,我需要听广播AudioManager.ACTION_AUDIO_BECOMING_NOISY.还行吧!这里没问题.
但是当用户再次插入耳机时,我需要取消静音设备.但是没有AudioManager.ACTION_AUDIO_BECOMING_NOISY相反的广播.我不知道耳机何时再次插上.
一个解决办法是定期检查是否AudioManager.isWiredHeadsetOn()是true但这似乎并没有得到很好的解决了我.
有没有办法检测用户何时在设备上插入耳机?
编辑:我试图用Intent.ACTION_HEADSET_PLUG这种方式,但它没有用.
在manifest.xml中我放了:
<receiver android:name=".MusicIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
以下是我的代码MusicIntentReceiver.java:
public class MusicIntentReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent intent) {
AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
Log.d("Let's turn the sound on!");
//other things to un-mute the streams
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试其他任何解决方案?