Question

I have an app that can take you from silent/vibrate and to normal ringer mode, but when i try to put you back in the mode i took you out of, i always end up in the silent mode. Can anyone see why?

First i store the current ringstate as an integer with checkCurrentSoundState(), then i try to put the mobile back to the state i saved there when the call is over, but by the time it is over, the value i stored is gone and has been reset to 0. Anyone got some good tips on how to overcome this problem?

 @Override

    public void onReceive(Context context, Intent intent) {

            AudioManager maudio = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
            this.context = context;
            bundle = intent.getExtras();
            if (bundle == null)
                return;

            state = bundle.getString(TelephonyManager.EXTRA_STATE);
            Log.i("IncomingCall", "State: " + state);

            if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING) || TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals("EXTRA_STATE_RINGING")) {
                checkCurrentSoundState();
                setupDatabase();

                Bundle bundle = intent.getExtras();
                String phoneNr = bundle.getString("incoming_number");

                for (int i = 0; i < allowedThrough.size(); i++) {
                    if (PhoneNumberUtils.compare(allowedThrough.get(i), phoneNr)){
                        goToNormal();
                    }
                }
            }
            System.out.println("current ringstate is now " + ringcheck);

            if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
                returnToPreviousState();
        }

private void checkCurrentSoundState() {

        AudioManager maudio = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
        ringcheck = maudio.getRingerMode();
    }

private void returnToPreviousState() {

        AudioManager maudio = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

        if (ringcheck == AudioManager.RINGER_MODE_SILENT)
            maudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        else if (ringcheck == AudioManager.RINGER_MODE_NORMAL) 
            maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        else if (ringcheck == AudioManager.RINGER_MODE_VIBRATE) 
            maudio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    }
Was it helpful?

Solution

You need to save the ringer mode in SharedPreference

@Override

public void onReceive(Context context, Intent intent) {

        AudioManager maudio = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
        SharedPreferences pref = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        this.context = context;
        bundle = intent.getExtras();
        if (bundle == null)
            return;

        state = bundle.getString(TelephonyManager.EXTRA_STATE);
        Log.i("IncomingCall", "State: " + state);

        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING) || TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals("EXTRA_STATE_RINGING")) {
            editor.putInt("ringer_mode", maudio.getRingerMode());
            editor.commit();
            setupDatabase();

            Bundle bundle = intent.getExtras();
            String phoneNr = bundle.getString("incoming_number");

            for (int i = 0; i < allowedThrough.size(); i++) {
                if (PhoneNumberUtils.compare(allowedThrough.get(i), phoneNr)){
                    goToNormal();
                }
            }
        }
        System.out.println("current ringstate is now " + ringcheck);

        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
            maudio.setRingerMode(pref.getInt("ringer_mode", 0));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top