我正在创建一个呼叫应用程序。
这是适用于 android 4.0 和 5.0 的自动答案;而当我有来电时,接听电话按钮有效,但在 android 6.0 上无效。
我测试了这篇文章的答案,但它也不起作用:在 Android 6.0 中接听来电
传入活动:
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.imgaccept:
{
if (Build.VERSION.SDK_INT >= 21) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Runtime.getRuntime().exec( "input keyevent " + KeyEvent.KEYCODE_HEADSETHOOK );
Intent intent = new Intent(getApplicationContext(), OutGoing.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CommonMethods.OUTGOING_NUMBER, savedNumber);
startActivity(intent);
finish();
}
catch (Throwable t) {
}
}
}).start();
}
else {
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
Intent intent = new Intent(this, OutGoing.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CommonMethods.OUTGOING_NUMBER, savedNumber);
startActivity(intent);
finish();
}
break;
}
case R.id.imgdecline:
{
CommonMethods.rejectCall(this);
finish();
break;
}
default:
break;
}
}