我正在我的活动中执行以下代码来设置铃声模式和值:
void setRinger(int value) {
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
manager.setStreamVolume(AudioManager.STREAM_RING, value, AudioManager.FLAG_PLAY_SOUND);
}
void setSilent() {
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
manager.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_SHOW_UI);
}
void setVibrate() {
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
manager.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_VIBRATE);
}
setRinger()设置铃声值并播放声音作为确认。
setSilent()设置静默模式并显示“ toast”作为确认。
setVibrate()设置振动模式,但手机没有振动作为确认。
我正在使用HTC Wildfire S(Android 2.3.5)。当我使用手机上的按钮更改铃声音量时,振动正常。
我究竟做错了什么?有任何特权吗?我尝试将android.permission.VIBRATE添加到清单-没有帮助。
允许的清单:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DroidUtilityActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
Logcat输出(我切断了大多数日志)只是为了证明代码被称为:
D / dalvikvm(13742):GC_CONCURRENT释放279K,47%释放2992K / 5639K,外部0K / 0K,暂停5ms + 9ms
D / dalvikvm(11205):GC_EXPLICIT释放了10K,47%释放了2862K / 5379K,外部0K / 0K,暂停了153ms
D / AudioManager(13742):setStreamVolume(streamType:2,索引:3,标志:4)
V /测试(13742):设定器
D / InputManagerService(131):窗口已聚焦,忽略了com.android.internal.view.IInputMethodClient$Stub$Proxy@40900198的聚焦增益
D / VolumePanel(131):准备声音
D / VolumePanel(131):准备声音/系统/媒体/音频/ui/Volume-1.mp3
D / VolumePanel(131):准备声音/系统/媒体/音频/ui/Volume-2.mp3
...
E / AudioService(131):[AudioHandler] syncSetIndex调用setStreamVolumeIndex isHeadsetPlugged():true
D / StatusBarService(219):updateIcon插槽=体积索引= 15 viewIndex = 9 old = StatusBarIcon(pkg = com.android.systemui id = 0x7f0200c7 level = 0可见= false num = 0)icon = StatusBarIcon(pkg = com.android .systemui id = 0x7f0200c6 level = 0可见= true num = 0)
D / AudioManager(13742):setStreamVolume(streamType:2,index:0,标志:1)
V /测试(13742):setsilent
D / InputManagerService(131):窗口已聚焦,忽略了com.android.internal.view.IInputMethodClient$Stub$Proxy@40b235c8的聚焦增益
D / AudioService(131):[AudioHandler] persistentVolumeForHeadset名称:volume_system_headset,mIndex_Headset:0,mLastAudibleIndex_Headset:7,当前:true,lastAudible:true
D / AudioService(131):[AudioHandler] persistentVolumeForHeadset名称:volume_ring_headset,mIndex_Headset:0,mLastAudibleIndex_Headset:4,当前:true,lastAudible:true
D / AudioService(131):[AudioHandler] persistentVolumeForHeadset名称:volume_ring_headset,mIndex_Headset:0,mLastAudibleIndex_Headset:4,当前:true,lastAudible:true
D / AudioService(131):[AudioHandler] persistentVolumeForHeadset名称:volume_system_headset,mIndex_Headset:0,mLastAudibleIndex_Headset:7,当前:true,lastAudible:true
D / StatusBarService(219):updateIcon插槽=体积索引= 15 viewIndex = 9 old = StatusBarIcon(pkg = com.android.systemui id = 0x7f0200c6 level = 0可见= true num = 0)icon = StatusBarIcon(pkg = com.android .systemui id = 0x7f0200c7 level = 0可见= true num = 0)
D / AudioManager(13742):setStreamVolume(streamType:2,index:0,标志:16)
V /测试(13742):setvibrate
D / InputManagerService(131):窗口已经聚焦,忽略了com.android.internal.view.IInputMethodClient$Stub$Proxy@40c2d508的聚焦增益
D / StatusBarPolicy(219):onSignalStrengthsChanged
D / StatusBarService(219):updateIcon插槽=电话信号索引= 20 viewIndex = 13旧= StatusBarIcon(pkg = com.android.systemui id = 0x7f0200d0级别= 0可见= true num = 0)图标= StatusBarIcon(pkg = com.android .systemui id = 0x7f0200d0 level = 0可见= true num = 0)
I / EventLogService(273):从1326197648488(日志),1326197648488(数据)汇总
D / LocationManager(131):_handle消息:TYPE_LOCATION_CHANGED
D / GpsLocationProvider(131):[handleMessage]消息:7
D / GpsLocationProvider(131):[handleMessage] UPDATE_LOCATION
D / GpsLocationProvider(131):handleUpdateLocation
D / LocationManager(3422):_handle消息:TYPE_LOCATION_CHANGED
D / LocationManager(327):_handle消息:TYPE_LOCATION_CHANGED
D / LocationManager(6893):_handle消息:TYPE_LOCATION_CHANGED
D / LocationManager(327):_handle消息:TYPE_LOCATION_CHANGED
最佳答案
我和您有同样的问题(使用HTC Desire HD)。在我的情况下,同时使用两个标志(AudioManager.FLAG_VIBRATE和AudioManager.FLAG_SHOW_UI)可以解决问题–手机振动并出现振动对话框。但是,当我仅使用AudioManager.FLAG_VIBRATE标志手机时不会振动。在我的代码下面:
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
int currentStreamRing = manager.getStreamVolume(AudioManager.STREAM_RING);
manager.setStreamVolume(AudioManager.STREAM_RING,currentStreamRing,AudioManager.FLAG_VIBRATE+AudioManager.FLAG_SHOW_UI);