The mechanism for realizing the sound of the Android button vibrate button

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px">When the button wants to call the System's vibration service, The first must be androidmainfest.xml inside plus the corresponding permissions</span></p></p><p><p><span style="font-size:18px"><uses-permission android:name= "android.permission.VIBRATE"/><br></span></p></p><p><p><span style="font-size:18px">Then is <span style="white-space:pre"> </span>vibrator vibrator = (vibrator) Getsystemservice (vibrator_service);<br>Vibrator.vibrate (3000);<br>Set the vibrate vibration period<br>Vibrator.vibrate (new long[]{1000,2000,3000,4000}, 0);</span></p></p><p><p><span style="font-size:18px">here, I found a good way to write a vibrating method on the Internet.</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package Com.lxb.switchdemo;import Android.app.activity;import Android.app.service;import android.os.Vibrator; Import android.widget.linearlayout;/** * Phone Shake tool class * * @author Administrator * */public class vibratorutil {/** * fi NAL activity activity: Invoke the activity instance of this method long milliseconds: the length of the vibration, in milliseconds * long[] pattern: custom Vibration Mode. The meaning of the numbers in the array is [stationary duration, length of vibration, length of rest, duration of vibration ...] ] The length of the unit is milliseconds * Boolean isrepeat: whether repeated vibrations, if true, repeated vibrations, if false, only shakes once */public static void vibrate (final activity activity, Lo ng Milliseconds) {vibrator vib = (vibrator) activity.getsystemservice (service.vibrator_service); vib.vibrate ( milliseconds);} public static void Vibrate (final activity activity, long[] pattern,boolean isrepeat) {vibrator vib = (vibrator) activity.g Etsystemservice (service.vibrator_service); vib.vibrate (pattern, isrepeat? 1:-1);}}</pre></pre>of course, when you invoke it in your activity, you only need to add it under the Onclick.<p><p></p></p><p><p><span style="font-size:18px">Vibratorutil.vibrate (switch_demoactivity.this, 100);</span></p></p><p><p><span style="font-size:18px">The simple vibration mechanism can be realized<br><br></span></p></p><p><p><span style="font-size:18px">Down to summarize the key to the sound mechanism of implementation,</span></p></p><p><p><span style="font-size:18px"></span></p></p> <ol start="1" class="dp-j" style="padding:0px; border:none; list-style-position:initial; color:rgb(92,92,92); font-family:Consolas,‘Courier New‘,Courier,mono,serif; line-height:26px; margin:0px 0px 1px 45px!important"> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">public </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span> <span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">class </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> mainactivity  </span> Span class= "keyword" style= "margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">extends </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> activity {     </span> </span></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; Background-color:rgb (248,248,248) "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">    <span class=" keyword " style=" margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">private </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> button mbutton01;     </span> </span> </li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">    <span class=" keyword " style=" margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">private </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> soundpool sp; </span> <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb (0,130,0); background-color:inherit">/ /declaration of a soundpool  </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">    </span> </span> </li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">     <span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">private </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span> <span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">int </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> music; </span> <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb (0,130,0); background-color:inherit">/ /define an integral type with load (); to set suondid   </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">   </span> </span></span></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"></span></li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="annotation" style="margin:0px; padding:0px; border:none; color:rgb(100,100,100); background-color:inherit">@Override</span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">   </span></span></li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">    <span class=" keyword " style=" margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">public </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span> <span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">void </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> oncreate (Bundle  Savedinstancestate)  {    </span> </span> </li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; Background-color:rgb (248,248,248) "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">   </span> </li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">        <span class=" keyword " style=" margin:0 px padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">super </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">.oncreate (savedInstanceState);     </span> </span> </li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">Setcontentview (r.layout.activity_main);</span></li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">mbutton01= (Button) Findviewbyid (r.id.mbutton01);</span></li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">sp=<span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">New</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">Soundpool (</span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">Ten</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">, audiomanager.stream_system,</span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">5</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">);</span></span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">the <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">first parameter is the maximum number of simultaneous data streams, the second data stream type, and the third is the sound quality</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></span></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">         music = sp.load (<span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">this </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">, r.raw.start,  </span> Span class= "number" style= "margin:0px; padding:0px; border:none; Color:rgb (192,0,0); Background-color:inherit ">1 </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit" );   < span> <span class=" Comment " style=" margin:0px; padding:0px; border:none; Color:rgb (0,130,0); Background-color:inherit ">//put your sound material into the res/raw, the 2nd parameter is the resource file, the 3rd is the priority of the music   </span> <span style=" margin:0px; padding:0px; border:none; Background-color:inherit ">   </span> </span></span></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; Background-color:rgb (248,248,248) "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">   </span> </li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">        mbutton01.setonclicklistener (<span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">new </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> onclicklistener () {     </span> </span> </li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; Background-color:rgb (248,248,248) "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">            <span class=" Annotation " style=" margin:0px; padding:0px; border:none; Color:rgb (100,100,100); Background-color:inherit "> @Override </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">    </span> </span> </li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none None None solid; border-left-width:3px; Border-left-color:rgb (108,226,108); List-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important "><span style=" margin:0px; padding:0px; border:none; color:black; Background-color:inherit ">           <span class=" Keyword " style=" margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">public </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span> <span class="keyword" style="margin:0px; padding:0px; border:none; Color:rgb (0,102,153); font-weight:bold; Background-color:inherit ">void </span> <span style="margin:0px; padding:0px; border:none; background-color:inherit"> onclick (View v)  {     </span> </span> </li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">Sp.play (music,<span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">0</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span></span><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit"><span class="number" style="margin:0px; padding:0px; border:none; color:rgb(192,0,0); background-color:inherit">1</span></span><span style="margin:0px; padding:0px; border:none; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">); </span></span></span></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}</span></li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}</span></li></li> <li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}</span></li></li> <li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">}</span></li></li> </ol>Raw is a new folder under the res, it is not necessary to compile the resource files can be directly used, of course, in order to avoid in some machines do not recognize the key sound of the day, it is best to convert the audio format needs to be loaded into Ogg format.<p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>The mechanism for realizing the sound of the Android button vibrate button</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.