Make an Android device vibrate with an activity












1















I'm trying to make the Android phone vibrate with a button click within an Activity. I did a search but some said it cannot be done with Activity as it is a Context class.



Here is what I have done.



In the manifest file:



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


Code:



public class SecurityActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{

public Vibrator v;
public void vibrateStart(View view){
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) v.vibrate(1000);
}

public void vibrateStop(View view){
if (v != null) v.cancel();
}
}


The onClick in the XML will call the method defined.










share|improve this question





























    1















    I'm trying to make the Android phone vibrate with a button click within an Activity. I did a search but some said it cannot be done with Activity as it is a Context class.



    Here is what I have done.



    In the manifest file:



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


    Code:



    public class SecurityActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener
    {

    public Vibrator v;
    public void vibrateStart(View view){
    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if (v != null) v.vibrate(1000);
    }

    public void vibrateStop(View view){
    if (v != null) v.cancel();
    }
    }


    The onClick in the XML will call the method defined.










    share|improve this question



























      1












      1








      1








      I'm trying to make the Android phone vibrate with a button click within an Activity. I did a search but some said it cannot be done with Activity as it is a Context class.



      Here is what I have done.



      In the manifest file:



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


      Code:



      public class SecurityActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener
      {

      public Vibrator v;
      public void vibrateStart(View view){
      v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      if (v != null) v.vibrate(1000);
      }

      public void vibrateStop(View view){
      if (v != null) v.cancel();
      }
      }


      The onClick in the XML will call the method defined.










      share|improve this question
















      I'm trying to make the Android phone vibrate with a button click within an Activity. I did a search but some said it cannot be done with Activity as it is a Context class.



      Here is what I have done.



      In the manifest file:



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


      Code:



      public class SecurityActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener
      {

      public Vibrator v;
      public void vibrateStart(View view){
      v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      if (v != null) v.vibrate(1000);
      }

      public void vibrateStop(View view){
      if (v != null) v.cancel();
      }
      }


      The onClick in the XML will call the method defined.







      android vibrate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 20 '15 at 18:13









      BSMP

      2,54952334




      2,54952334










      asked Dec 19 '15 at 23:41









      dilbertdilbert

      212




      212
























          2 Answers
          2






          active

          oldest

          votes


















          0














          At the end, it was the "Vibrate on touch" not being set in the Phone Setting.
          After set it to high, it is working.



          Now, I have another question. How could I set the Vibrate intensity inside the Apps ??






          share|improve this answer
























          • stackoverflow.com/questions/2888043/…

            – TouchBoarder
            Dec 20 '15 at 20:00











          • Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

            – TouchBoarder
            Dec 20 '15 at 20:08






          • 1





            To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

            – Ed Cottrell
            Dec 22 '15 at 4:48



















          0














          Use this method where you want, it will help you to vibrate the android device from our app.



          private void vibrate() {
          Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
          } else {
          v.vibrate(500); // deprecated in API 26
          }
          }


          add permission in manifest



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





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34376567%2fmake-an-android-device-vibrate-with-an-activity%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            At the end, it was the "Vibrate on touch" not being set in the Phone Setting.
            After set it to high, it is working.



            Now, I have another question. How could I set the Vibrate intensity inside the Apps ??






            share|improve this answer
























            • stackoverflow.com/questions/2888043/…

              – TouchBoarder
              Dec 20 '15 at 20:00











            • Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

              – TouchBoarder
              Dec 20 '15 at 20:08






            • 1





              To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

              – Ed Cottrell
              Dec 22 '15 at 4:48
















            0














            At the end, it was the "Vibrate on touch" not being set in the Phone Setting.
            After set it to high, it is working.



            Now, I have another question. How could I set the Vibrate intensity inside the Apps ??






            share|improve this answer
























            • stackoverflow.com/questions/2888043/…

              – TouchBoarder
              Dec 20 '15 at 20:00











            • Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

              – TouchBoarder
              Dec 20 '15 at 20:08






            • 1





              To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

              – Ed Cottrell
              Dec 22 '15 at 4:48














            0












            0








            0







            At the end, it was the "Vibrate on touch" not being set in the Phone Setting.
            After set it to high, it is working.



            Now, I have another question. How could I set the Vibrate intensity inside the Apps ??






            share|improve this answer













            At the end, it was the "Vibrate on touch" not being set in the Phone Setting.
            After set it to high, it is working.



            Now, I have another question. How could I set the Vibrate intensity inside the Apps ??







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 20 '15 at 19:46









            dilbertdilbert

            212




            212













            • stackoverflow.com/questions/2888043/…

              – TouchBoarder
              Dec 20 '15 at 20:00











            • Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

              – TouchBoarder
              Dec 20 '15 at 20:08






            • 1





              To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

              – Ed Cottrell
              Dec 22 '15 at 4:48



















            • stackoverflow.com/questions/2888043/…

              – TouchBoarder
              Dec 20 '15 at 20:00











            • Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

              – TouchBoarder
              Dec 20 '15 at 20:08






            • 1





              To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

              – Ed Cottrell
              Dec 22 '15 at 4:48

















            stackoverflow.com/questions/2888043/…

            – TouchBoarder
            Dec 20 '15 at 20:00





            stackoverflow.com/questions/2888043/…

            – TouchBoarder
            Dec 20 '15 at 20:00













            Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

            – TouchBoarder
            Dec 20 '15 at 20:08





            Look at the 'Related' question column at the right --> --> And next time... please do some research first before asking others to do the work for you;)

            – TouchBoarder
            Dec 20 '15 at 20:08




            1




            1





            To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

            – Ed Cottrell
            Dec 22 '15 at 4:48





            To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.

            – Ed Cottrell
            Dec 22 '15 at 4:48













            0














            Use this method where you want, it will help you to vibrate the android device from our app.



            private void vibrate() {
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
            v.vibrate(500); // deprecated in API 26
            }
            }


            add permission in manifest



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





            share|improve this answer






























              0














              Use this method where you want, it will help you to vibrate the android device from our app.



              private void vibrate() {
              Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
              } else {
              v.vibrate(500); // deprecated in API 26
              }
              }


              add permission in manifest



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





              share|improve this answer




























                0












                0








                0







                Use this method where you want, it will help you to vibrate the android device from our app.



                private void vibrate() {
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
                } else {
                v.vibrate(500); // deprecated in API 26
                }
                }


                add permission in manifest



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





                share|improve this answer















                Use this method where you want, it will help you to vibrate the android device from our app.



                private void vibrate() {
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
                } else {
                v.vibrate(500); // deprecated in API 26
                }
                }


                add permission in manifest



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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 7:30

























                answered Nov 13 '18 at 6:44









                AgilanbuAgilanbu

                1,2571319




                1,2571319






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34376567%2fmake-an-android-device-vibrate-with-an-activity%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues