是否可以使用首选项活动编写一个解决系统卷函数的工作?
就像说我想向用户输出一条消息,如果音量设置得太大,它会弹出一个吐司说“嘿音量太大”
我现在知道怎样做吐司我正在考虑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!
答案 0 :(得分:1)
跟踪音量水平并不像看起来那么简单......有许多不同的流,当用户离开您的应用时音量可以改变,用户可能会连接到具有自己音量控制的扬声器等。
可以选择定义音量按钮应影响的流:Activity.setVolumeControlStream()。
但我建议关注AudioManager,它充满了有用的方法和意图,如AudioManager.ACTION_AUDIO_BECOMING_NOISY。
如果您需要特定帮助,请发布您编写的相关代码和任何logcat错误。希望有所帮助!