在android marshmallow 6.0中无法接听来电

时间:2017-02-20 08:19:58

标签: android android-6.0-marshmallow android-permissions incoming-call

我正在创建一个通话应用。

这是适用于Android 4.0和5.0的自动回答;而当我有一个来电应答呼叫按钮工作,但它不适用于Android 6.0。

我测试了这篇文章的答案,但它也不起作用:Answer Incoming Call in Android 6.0

IncomingActivity:

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

1 个答案:

答案 0 :(得分:6)

To many research, finally i found solution

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

Add in manifest:

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

Accept on button click:

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

For Permission:

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

Have tested upto version Nougat.

Cheers!