是否可以使用偏好活动来解决系统音量功能?
比如说我想向用户输出一条消息,如果音量设置得太大,它会弹出一个吐司说“嘿,音量太大了”
我现在知道如何祝酒,我正在考虑 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!