-1

是否可以使用偏好活动来解决系统音量功能?

比如说我想向用户输出一条消息,如果音量设置得太大,它会弹出一个吐司说“嘿,音量太大了”

我现在知道如何祝酒,我正在考虑 pref 活动。只是在这里讨论想法。

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class volman extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dhd);

    DialogPreference dp = (DialogPreference) findPreference("mediavolume");
    dp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            SeekBar volumeBar = (SeekBar) findViewById(R.id.seekBar);
            final AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            volumeBar.setMax(manager
                    .getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
            volumeBar.setProgress(manager
                    .getStreamVolume(AudioManager.STREAM_SYSTEM));

            volumeBar
                    .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                            Toast.makeText(volman.this, "Lift off.... StartSpark", Toast.LENGTH_LONG).show();

                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            Toast.makeText(volman.this, "Now Stopping", Toast.LENGTH_LONG).show();

                        }
                        public void onProgressChanged(SeekBar seekBar,
                                int progress, boolean fromUser) {
                            manager.setStreamVolume(
                                    AudioManager.STREAM_SYSTEM, progress,
                                    AudioManager.FLAG_SHOW_UI);
                            Toast.makeText(volman.this, "Now going quiet", Toast.LENGTH_LONG).show();
                        }
                    });

            return false;
        }
    });
}

private DialogPreference findPreference(String string) {
    return null;
}
}

现在对于 logcat:

[2012-07-07 15:25:00 - volman4.0] Android Launch!
[2012-07-07 15:25:00 - volman4.0] adb is running normally.
[2012-07-07 15:25:00 - volman4.0] No Launcher activity found!
[2012-07-07 15:25:00 - volman4.0] The launch will only sync the application package on the device!
[2012-07-07 15:25:00 - volman4.0] Performing sync
[2012-07-07 15:25:00 - volman4.0] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'ICS'
[2012-07-07 15:25:00 - volman4.0] Uploading volman4.0.apk onto device 'emulator-5554'
[2012-07-07 15:25:00 - volman4.0] Installing volman4.0.apk...
[2012-07-07 15:25:10 - volman4.0] Success!
[2012-07-07 15:25:11 - volman4.0] /volman4.0/bin/volman4.0.apk installed on device
[2012-07-07 15:25:11 - volman4.0] Done!
4

1 回答 1

1

跟踪音量水平并不像看起来那么简单......有许多不同的流,当用户离开您的应用程序时,音量可能会改变,用户可能会通过自己的音量控制连接到扬声器等。

有一个选项可以定义音量按钮应该影响哪个流:Activity.setVolumeControlStream()

但我建议专注于AudioManager,它充满了有用的方法和意图,如AudioManager.ACTION_AUDIO_BECOMING_NOISY

如果您需要具体帮助,请发布您编写的相关代码和任何 logcat 错误。希望有帮助!

于 2012-07-07T19:11:44.410 回答