简体   繁体   中英

Allow phone to vibrate when screen turns off

I am looking for a way to allow my program to make the phone vibrate after the screen has turned off from timing out. I have done lots of research, and have not found something that works. I have looked at the PowerManager class and more specifically the WakeLock mechanism. From the sound of many posts, I would need to use the PARTIAL_WAKE_LOCK variable of the WakeLock class.

PARTIAL_WAKE_LOCK - Wake lock that ensures that the CPU is running.

However, I cannot get it to vibrate the phone when the screen turns off. I know I am using the WakeLock correctly because I can get SCREEN_DIM_WAKE_LOCK to work. Is PARTIAL_WAKE_LOCK what I am looking for?

 @Override

    public void onCreate() {

        super.onCreate();

        // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

        filter.addAction(Intent.ACTION_SCREEN_OFF);

        BroadcastReceiver mReceiver = new ScreenReceiver();

        registerReceiver(mReceiver, filter);

    }



    @Override

    public void onStart(Intent intent, int startId) {

        boolean screenOn = intent.getBooleanExtra("screen_state", false);

        if (!screenOn) {

            // Get instance of Vibrator from current Context
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
           // This example will cause the phone to vibrate "SOS" in Morse Code
           // In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash"
           // There are pauses to separate dots/dashes, letters, and words
           // The following numbers represent millisecond lengths
            int dot = 200;      // Length of a Morse Code "dot" in milliseconds
           int dash = 500;     // Length of a Morse Code "dash" in milliseconds
            int short_gap = 200;    // Length of Gap Between dots/dashes
           int medium_gap = 500;   // Length of Gap Between Letters
           int long_gap = 1000;    // Length of Gap Between Words
            long[] pattern = {
             0,  // Start immediately
             dot, short_gap, dot, short_gap, dot,    // s
             medium_gap,
             dash, short_gap, dash, short_gap, dash, // o
            medium_gap,
            dot, short_gap, dot, short_gap, dot,    // s
           long_gap
           };

           // Only perform this pattern one time (-1 means "do not repeat")
          v.vibrate(pattern, -1);


        } else {

            // YOUR CODE

        }

    }

note u must Add the uses-permission line to your Manifest.xml file, outside of the block.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<uses-permission android:name="android.permission.VIBRATE"/>

note : you must also test this code on a real phone , emulator can't viberate

对我来说,解决方案是直接使用没有模式的振动,因此不必使用PowerManager来唤醒锁定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM