Can't answer incoming call in android marshmallow 6.0

雨燕双飞 提交于 2019-11-29 05:51:57

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!

Shreyas Malhotra

Solved! Dialer app had "Show Notifications" box unticked.

Not sure how that happened.

For anyone experiencing the same issue:

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

You need to tick the show notification box just go to

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!