14

我正在创建一个呼叫应用程序。

这是适用于 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;
    }
}
4

3 回答 3

7

经过大量研究,最后我找到了解决方案

NotificationCall.java

public class NotificationCall extends NotificationListenerService {

@RequiresApi(api = Build.VERSION_CODES.KITKAT)

static StatusBarNotification mysbn;
Context context;

public StatusBarNotification[] getActiveNotifications() {
    return super.getActiveNotifications();
}

public void onCreate() {
    super.onCreate();
    this.context = getApplicationContext();
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    mysbn = sbn;
    try {

        String packageName = sbn.getPackageName();
        Intent intent = new Intent("Msg");
        intent.putExtra("package", packageName);
        LocalBroadcastManager.getInstance(this.context).sendBroadcast(intent);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

在清单中添加:

 <service
        android:name=".NotificationCall"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

接受按钮点击:

button.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onClick(View v) {
            try {
                for (MediaController mediaController : ((MediaSessionManager) getApplicationContext().getSystemService("media_session")).getActiveSessions(new ComponentName(getApplicationContext(), NotificationCall.class))) {
                    if ("com.android.server.telecom".equals(mediaController.getPackageName())) {
                        mediaController.dispatchMediaButtonEvent(new KeyEvent(1, 79));
                        return;
                    }
                }
            } catch (SecurityException e2) {
                e2.printStackTrace();
            }
        }
    });

您需要勾选显示通知框,只需转到

Settings > Apps > All > Dialer > Check the notification box

对于许可:

  if (Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName()))
    {

    } else
        {
        Intent i = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }

已测试最高版本 Nougat

干杯!

于 2017-08-05T11:37:25.547 回答
0

您需要勾选显示通知框,只需转到

Settings > Apps > All > Dialer > Check the notification box

于 2017-08-12T11:49:43.060 回答
0

解决了!拨号器应用程序未勾选“显示通知”框。

不知道那是怎么发生的。

对于遇到相同问题的任何人:

设置 > 应用程序 > 全部 > 拨号器 > 选中通知框。

于 2017-08-11T16:15:25.777 回答