How to let the phone vibrate when the screen is locked

Auto 46 Reputation points
2021-06-30T13:55:26.947+00:00

To let vibrate the Phone every second I can do this way:

            Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                    Vibration.Vibrate(500);
                    return true;
            });

The permissions for the App are at list the following:

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

It works fine.
Anyway, if i lock the phone, the timer is always triggered, but the phone doesn't vibrate any more, until I unlock the screen.
Is there a way to be able to let vibrate the phone even if the screen is locked ?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-07-01T06:20:18.67+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To keep the vibrate process working even when the screen is locked, try to PowerManager to Partial mode which ensures that the CPU is running when the screen will be turned off.

       PowerManager powerManager = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService);  
       WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "MyWakeLock");  
       wakeLock.Acquire();  
         
       //wakeLock.Release(); //close the mode  
    

    If you're testing a Xamarin.Forms project, you could use DependencyService to execute the native code in the shared project.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Auto 46 Reputation points
    2021-07-07T15:55:20.017+00:00

    II tried to run the Xamarin.Android Foreground Service Sample:

    https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/applicationfundamentals-servicesamples-foregroundservicedemo/

    but the sample itself doesn't work, so I opened an issue:

    https://github.com/xamarin/monodroid-samples/issues/325

    Look forward to see this example working, so I could try to integrate the Vibrate command from Xamarin.Essentials and see if it works when the screen is locked.

    I will update here the result.

    1 person found this answer helpful.

  2. Auto 46 Reputation points
    2021-07-05T10:11:18.537+00:00

    I tried to use your suggestion but it doesn't work.
    When the screen is locked the mobile doesn't vibrate any more.
    I also tried to use the WakeLockFlags.Full flag, instead of WakeLockFlags.Partial, but the behaviour is the same.
    Any other tips?
    Thanks.

    public interface IWakeLockService
    {
        void SetupWakeLock(long duration);
    }
    
    public class WakeLockService : IWakeLockService
    {
        public void SetupWakeLock(long duration)
        {
            try
            {
                PowerManager powerManager = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService);
    
                // Is the screen off ?
                if (powerManager.IsInteractive == false)
                {
                    // Yes
                    WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "MyWakeLock");
                    wakeLock.Acquire(duration);
                    System.Diagnostics.Debug.WriteLine($"wakeLock.Acquire() done");
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"SetupWakeLock() Exception --> {ex.Message}");
            }
        }
    }
    

    Usage:

    public static void VibrateMobile(double duration = 500)
    {
        try 
        {            
            if (Device.RuntimePlatform == Device.Android) 
            { 
                DependencyService.Get<IWakeLockService>().SetupWakeLock((long)(duration + 100)); 
            }            
    
            Vibration.Vibrate(TimeSpan.FromMilliseconds(duration));
        }
        catch (FeatureNotSupportedException ex1) 
        {
            /* Feature not supported on device */
        }
        catch (Exception ex)                    
        {
            /* Other error has occurred.       */
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.