0

我有一个应用程序可以让你从静音/振动到正常铃声模式,但是当我试图让你回到我带你出去的模式时,我总是以静音模式结束。谁能明白为什么?

首先,我使用 checkCurrentSoundState() 将当前 ringstate 存储为整数,然后我尝试将手机恢复到通话结束时保存在那里的状态,但是当通话结束时,我存储的值已经消失了已重置为 0。有人对如何克服这个问题有一些好的建议吗?

 @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);
    }
4

1 回答 1

1

您需要在 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));
    }
于 2013-07-25T21:40:59.453 回答