无法在Android棉花糖6.0中接听来电 [英] Can't answer incoming call in android marshmallow 6.0

查看:101
本文介绍了无法在Android棉花糖6.0中接听来电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建通话应用程序.

I'm creating a calling app.

这里是自动答案,可在android 4.0和5.0上使用;反之,当我有来电接听时,通话按钮有效,但在android 6.0上不起作用.

Here's Auto answer which works on android 4.0 and 5.0; whereas when i have an incoming call answer call button works but it doesn't work on android 6.0.

我测试了此帖子的答案,但也无法正常工作:Android 6.0中的来电

I tested answer of this post but it doesn't work too : Answer Incoming Call in 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;
    }
}

推荐答案

对于许多研究,终于找到了解决方法

To many research, finally i found solution

NotificationCall.java

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

您需要在显示通知框上打钩,只需转到

You need to tick the show notification box just go to

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

已经测试了最新版本的牛轧糖.

干杯!

这篇关于无法在Android棉花糖6.0中接听来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆