开发者

Setting Vibrate while in a lock state Android

开发者 https://www.devze.com 2023-03-15 15:47 出处:网络
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 tho

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.getSystemServ开发者_开发百科ice(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.

  1. Can settings be changed when in a lock state?

  2. What am I missing for that to occur? I bet it's just something so simple I'm over looking.

Thanks, Kelly


Currently these are the phone states I'm using for this to work. I think the thing that makes it work is the modify phone state. Hopefully, this can help someone else out.

BTW. It really a pain some of the description in the google docs for this. Does anyone know of a better place that (I know this is a funny question because they're google's docs) describes the phone's user permissions.

One other thing. After some reading, it has been noted that the modify phone state has changed some with future behavior. Basically some hidden / undocumented classes / Interfaces can be used like in 2.2.

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

<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"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号