更改铃声模式在android中第一次无法正常工作

时间:2017-03-21 07:29:13

标签: android android-audiomanager telephonymanager android-broadcastreceiver

以下是我的callreceiver brodcastreceiver,它将在任何电话通话时拨打电话。问题是当我打电话时,我正在将铃声模式改为静音。但它不是第一次工作。然后在那之后工作正常。 我尝试了很多次,但在打电话时仍然没有第一次工作。帮我解决这个问题。

CallReceiver.java

public class CallReceiver extends BroadcastReceiver {

    public static String TAG = "CallReceiver";

    AudioManager audioManager;
    TelephonyManager telephony;
    ComponentName receiver;
    PackageManager pm;

    @Override
    public void onReceive(final Context context, Intent intent) {

        Log.e(TAG, "onReceive ");
        receiver = new ComponentName(context, RingerModeStateChangeReceiver.class);
        pm = context.getPackageManager();

        audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
        telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String Number) {
                super.onCallStateChanged(state, Number);
                Log.e(TAG, "Number: " + Number);

                if (state == 2) {
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                } else {
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                }


            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

0 个答案:

没有答案