使用InCallService静默拒绝通话

时间:2018-09-01 16:56:54

标签: android

我正在尝试通过将我的应用设置为默认拨号器应用来拒绝使用InCallService的来电。它可以很好地拒绝通话,但问题是,如果手机处于振动模式,它会使其振动一些,我已经探索了类,但是我找不到任何使铃声静音的方法,这是我实现InCallService服务的代码

    public class myService extends InCallService {

    private String TAG="myService";
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"onBind");
        return super.onBind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG,"onUnbind");
        return super.onUnbind(intent);

    }

    public myService() {
        super();
    }

    @Override
    public void onCallAudioStateChanged(CallAudioState audioState) {

        super.onCallAudioStateChanged(audioState);
        Log.d(TAG,"onCallAudioStateChanged");

    }

    @Override
    public void onBringToForeground(boolean showDialpad) {
        super.onBringToForeground(showDialpad);
        Log.d(TAG,"onBringToForeground");

    }

    @Override
    public void onCallAdded(Call call) {
        super.onCallAdded(call);

        call.reject(false,"ds");

    }  
}

能否请您告诉我如何静默拒绝通话?

2 个答案:

答案 0 :(得分:1)

如果要将您的应用程序设置为默认拨号程序应用程序,请尝试实现CallScreeningService API。该API将允许您在系统开始振铃之前拦截和阻止呼叫。

答案 1 :(得分:0)

我建议您更改方法。

第1步::创建广播接收器

public class CallReceiver extends BroadcastReceiver {

    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        rejectCall();
    }

    public void rejectCall() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
                if (telecomManager != null) {
                    telecomManager.endCall();
                }
            } else {
                rejectCallViaTelephonyManager();
            }
            Log.d(TAG, "call ended successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void rejectCallViaTelephonyManager() {
        ITelephony telephonyService = getTelephonyService(context);
        if (telephonyService != null) {
            telephonyService.silenceRinger();
            telephonyService.endCall();
        }
    }

    private ITelephony getTelephonyService(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Class c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            return (ITelephony) m.invoke(tm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

第2步:将此接收器添加到清单文件中

<receiver
    android:name=".CallReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="999">
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

步骤3:,您还需要添加权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />