在Android 6.0中接听来电

在Android 6.0中接听来电,android,android-6.0-marshmallow,android-permissions,Android,Android 6.0 Marshmallow,Android Permissions,在Android 6.0中,有没有任何方法可以在没有root权限的情况下以编程方式接听来电?我尝试了以下方法: -这样我就可以结束通话了。但接听电话需要android.permission.MODIFY_PHONE_STATE,android 6.0中没有提供给第三方应用程序 。这似乎根本不起作用 希望这能帮助一些人:) AcceptCallActivity.java import android.app.Activity; import android.app.KeyguardManager;

在Android 6.0中,有没有任何方法可以在没有root权限的情况下以编程方式接听来电?我尝试了以下方法:

  • -这样我就可以结束通话了。但接听电话需要android.permission.MODIFY_PHONE_STATE,android 6.0中没有提供给第三方应用程序
  • 。这似乎根本不起作用

  • 希望这能帮助一些人:)

    AcceptCallActivity.java

    import android.app.Activity;
    import android.app.KeyguardManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.media.AudioManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.telephony.TelephonyManager;
    import android.view.KeyEvent;
    import android.view.WindowManager;
    
    import java.io.IOException;
    import java.util.logging.Logger;
    
    
    public class AcceptCallActivity extends Activity {
    
        private static Logger logger = Logger.getLogger(String.valueOf(AcceptCallActivity.class));
    
        private static final String MANUFACTURER_HTC = "HTC";
    
        private KeyguardManager keyguardManager;
        private AudioManager audioManager;
        private CallStateReceiver callStateReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            registerCallStateReceiver();
            updateWindowFlags();
            acceptCall();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            if (callStateReceiver != null) {
                unregisterReceiver(callStateReceiver);
                callStateReceiver = null;
            }
        }
    
        private void registerCallStateReceiver() {
            callStateReceiver = new CallStateReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
            registerReceiver(callStateReceiver, intentFilter);
        }
    
        private void updateWindowFlags() {
            if (keyguardManager.inKeyguardRestrictedInputMode()) {
                getWindow().addFlags(
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            } else {
                getWindow().clearFlags(
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            }
        }
    
        private void acceptCall() {
    
            // for HTC devices we need to broadcast a connected headset
            boolean broadcastConnected = MANUFACTURER_HTC.equalsIgnoreCase(Build.MANUFACTURER)
                    && !audioManager.isWiredHeadsetOn();
    
            if (broadcastConnected) {
                broadcastHeadsetConnected(false);
            }
    
            try {
    
                try {
                   // logger.debug("execute input keycode headset hook");
                    Runtime.getRuntime().exec("input keyevent " +
                            Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
    
                } catch (IOException e) {
                    // Runtime.exec(String) had an I/O problem, try to fall back
                //    logger.debug("send keycode headset hook intents");
                    String enforcedPerm = "android.permission.CALL_PRIVILEGED";
                    Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                            Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                                    KeyEvent.KEYCODE_HEADSETHOOK));
                    Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                            Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                                    KeyEvent.KEYCODE_HEADSETHOOK));
    
                    sendOrderedBroadcast(btnDown, enforcedPerm);
                    sendOrderedBroadcast(btnUp, enforcedPerm);
                }
            } finally {
                if (broadcastConnected) {
                    broadcastHeadsetConnected(false);
                }
            }
        }
    
        private void broadcastHeadsetConnected(boolean connected) {
            Intent i = new Intent(Intent.ACTION_HEADSET_PLUG);
            i.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
            i.putExtra("state", connected ? 1 : 0);
            i.putExtra("name", "mysms");
            try {
                sendOrderedBroadcast(i, null);
            } catch (Exception e) {
            }
        }
    
        private class CallStateReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        }
    }
    
    已经测试了高达版本的棉花糖。


    干杯

    在进行了大量搜索之后,我发布了我在Orasi中使用的代码。希望这将帮助许多程序员。。。注意:此代码是从具有其他功能的软件中提取的,因此某些代码可能不会在所有软件中使用

    首先,在所有Android版本上应答和结束呼叫的功能(没有AIDL!):


    舱单:

        <service android:name=".NotificationReceiverService"
                 android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
                 android:enabled="true"
                 android:exported="true" >
                <intent-filter>
                    <action android:name="android.service.notification.NotificationListenerService" />
                </intent-filter>
        </service>
    
    
    

    
    
    问候


    编辑,我没有使用好的帐户

    @JagdeepSingh:是的,它也在后台工作。我如何从服务中调用它,我尝试了调用,但GUI什么都没有,即使我可以回答呼叫,我也无法在6.0上使用它。它正在制作棒棒糖。任何人都可以在Marshmellow设备上以编程方式接听电话吗?Thx。@PrabhuPrakash于2016年11月发布了安全路径更新版本。功能在已接收此更新的设备上不起作用。我已经提出了我的担忧。您也可以通过@Hiteshsapra check complete类申请
    /////////////////////////////////////////////////////////////////////////////////////
    // Controle le téléphone en utilisant des primitives selon les versions d'OS
    private void PhoneControl(int nControl) {
        if(nControl == PHONE_END_CALL) { // End call, all Android version
            try {
                TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                if(tm == null)
                    return;
                tm.getClass().getMethod("endCall").invoke(tm);
                bIsEnding = true;
            } catch (Exception e) { /* Do Nothing */ }
        }
    
        if(nControl == PHONE_ACCEPT_CALL) { // Accept phone call
            if(!bCallAccepted) { // Call déjà accepté => pas d'action (évite double action)
                bCallAccepted = true;
                if(Build.VERSION.SDK_INT >= 26) { // Pris en charge Android >= 8.0
                    if(checkSelfPermission("android.permission.ANSWER_PHONE_CALLS") == PackageManager.PERMISSION_GRANTED) {
                        TelecomManager tm = (TelecomManager) this.getSystemService(Context.TELECOM_SERVICE);
                        if(tm != null)
                            tm.acceptRingingCall();
                    }
                }
                if(Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 26) { // Hangup in Android 6.x and 7.x
                    MediaSessionManager mediaSessionManager =  (MediaSessionManager) getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE);
                    if(mediaSessionManager != null) {
                        try {
                            List<android.media.session.MediaController> mediaControllerList = mediaSessionManager.getActiveSessions
                                    (new ComponentName(getApplicationContext(), NotificationReceiverService.class));
    
                            for (android.media.session.MediaController m : mediaControllerList) {
                                if ("com.android.server.telecom".equals(m.getPackageName())) {
                                    m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
                                    m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
                                    break;
                                }
                            }
                        } catch (Exception e) { /* Do Nothing */ }
                    }
                }
                if(Build.VERSION.SDK_INT < 23) { // Prend en charge jusqu'à Android 5.1
                    try {
                        if(Build.MANUFACTURER.equalsIgnoreCase("HTC")) { // Uniquement pour HTC
                            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                            if(audioManager!=null && !audioManager.isWiredHeadsetOn()) {
                                Intent i = new Intent(Intent.ACTION_HEADSET_PLUG);
                                i.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
                                i.putExtra("state", 0);
                                i.putExtra("name", "Orasi");
                                try {
                                    sendOrderedBroadcast(i, null);
                                } catch (Exception e) { /* Do Nothing */ }
                            }
                        }
                        Runtime.getRuntime().exec("input keyevent " +
                                Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
                    } catch (Exception e) {
                        // Runtime.exec(String) had an I/O problem, try to fall back
                        String enforcedPerm = "android.permission.CALL_PRIVILEGED";
                        Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                                        KeyEvent.KEYCODE_HEADSETHOOK));
                        Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                                        KeyEvent.KEYCODE_HEADSETHOOK));
    
                        this.sendOrderedBroadcast(btnDown, enforcedPerm);
                        this.sendOrderedBroadcast(btnUp, enforcedPerm);
                    }
                }
            }
        }
    }
    
        if(Build.VERSION.SDK_INT >= 26) { // Permission necessaire
            if(checkSelfPermission("android.permission.ANSWER_PHONE_CALLS") != PackageManager.PERMISSION_GRANTED) {
                String szPermissions[] = {"android.permission.ANSWER_PHONE_CALLS"};
                requestPermissions(szPermissions, 0);
            }
        }
    
        if(Build.VERSION.SDK_INT >= 23 && bUseScreen ) { // Permission necessaire
            if(checkSelfPermission("android.permission.SYSTEM_ALERT_WINDOW") != PackageManager.PERMISSION_GRANTED) {
                Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivity(myIntent);
            }
            if(Build.VERSION.SDK_INT < 26) { // Permission pour Android 6.x et 7.x
                ContentResolver contentResolver = getContentResolver();
                String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
                String packageName = getPackageName();
                if (enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName)) {
                    Intent intent2 = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
                    startActivity(intent2);
                }
            }
        }
    
    package mss.micromega.pmignard.orasi;
    
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.os.Build;
    import android.service.notification.NotificationListenerService;
    
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @SuppressLint("OverrideAbstract")
    public class NotificationReceiverService extends NotificationListenerService {
        public NotificationReceiverService() {
        }
    }
    
        <service android:name=".NotificationReceiverService"
                 android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
                 android:enabled="true"
                 android:exported="true" >
                <intent-filter>
                    <action android:name="android.service.notification.NotificationListenerService" />
                </intent-filter>
        </service>
    
    <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>