I ve hit an interesting part in my studies with Android. I m as I go through I have created a timed PendingIntent to go off. Before it goes off, I lock the phone. When it goes off. I see it runs though and attempts to change the vibrate setting on my phone. Unfortunately it doesn t apply the setting.
code:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "VibrateTag");
wakeLock.acquire();
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audio.getRingerMode();
if(ringerMode != AudioManager.RINGER_MODE_NORMAL && item.getVibrate() && item.getVolume() != 0)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
final String INFO = "INFO";
int vibrateSetting = AudioManager.VIBRATE_SETTING_OFF;
if (item.getVolume() != 0)
{
if (item.getVibrate()) vibrateSetting = AudioManager.VIBRATE_SETTING_ON;
}
else
{
if (item.getVibrate()) vibrateSetting = AudioManager.VIBRATE_SETTING_ONLY_SILENT;
}
Log.e(INFO,"AReceiver attempting to update vibrate to " + vibrateSetting);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, vibrateSetting);
Log.e(INFO,"AReceiver updated vibrate to " + audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER));
wakeLock.release();
log when locked:
06-27 18:31:00.184: ERROR/INFO(3031): AReceiver attempting to update vibrate to 1
06-27 18:31:00.184: ERROR/INFO(3031): AReceiver updated vibrate to 0
Like I said when unlocked and in use, that log would read showing the change was successful where it would change its value to 1.
Now if I have my phone unlocked and I do this, it works. The Broadcast works (started on BootUp).
I ve even tried to lock down permissions to use with:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
Now with that in mind, I also tried doing these things with and without the wake lock.
当处于锁定状态时,环境会发生变化?
我为何没有这样做? 我只想看一看一看一看。
Thanks, Kelly