Android實(shí)現(xiàn)簡(jiǎn)單手機(jī)震動(dòng)效果
手機(jī)開發(fā)中,有時(shí)候我們需要使用震動(dòng)效果提示用戶當(dāng)前的軟件狀態(tài),下面以一個(gè)簡(jiǎn)單的例子實(shí)現(xiàn)這個(gè)功能。
1.新建Android應(yīng)用程序

2.在AndroidManifest.xml中申明需要使用的震動(dòng)權(quán)限
<uses-permission android:name="android.permission.VIBRATE" />
3.界面上添加按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動(dòng)1"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動(dòng)2"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動(dòng)3"/>
</LinearLayout>
4.源碼中處理按鈕效果
package com.sl.vibratordemo;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity
{
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
}
OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
switch (v.getId())
{
case R.id.btn1:
vibrator.vibrate(1000);//震動(dòng)1秒
break;
case R.id.btn2:
long[] pattern1 = {1000, 2000, 1000, 3000}; //等待1秒,震動(dòng)2秒,等待1秒,震動(dòng)3秒
vibrator.vibrate(pattern1, -1);
break;
case R.id.btn3:
long[] pattern2 = {1000, 500, 1000, 1000};
vibrator.vibrate(pattern2, 2);//-1表示不重復(fù), 如果不是-1, 比如改成1, 表示從前面這個(gè)long數(shù)組的下標(biāo)為1的元素開始重復(fù).
SystemClock.sleep(10000);//震動(dòng)10s以后停止
vibrator.cancel();
break;
default:
break;
}
}
};
}
5.運(yùn)行效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
這篇文章主要介紹了Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Android自定義評(píng)分控件的完整實(shí)例
在Android開發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分,下面這篇文章主要給大家介紹了關(guān)于Android自定義評(píng)分控件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
android 對(duì)話框彈出位置和透明度的設(shè)置具體實(shí)現(xiàn)方法
在android中我們經(jīng)常會(huì)用AlertDialog來(lái)顯示對(duì)話框。通過(guò)這個(gè)對(duì)話框是顯示在屏幕中心的。但在某些程序中,要求對(duì)話框可以顯示在不同的位置。2013-07-07
android使用ExpandableListView控件實(shí)現(xiàn)小說(shuō)目錄效果的例子
這篇文章主要介紹了android使用ExpandableListView控件實(shí)現(xiàn)小說(shuō)目錄效果的例子,還可以實(shí)現(xiàn)二級(jí)列表展示效果,需要的朋友可以參考下2014-07-07
Android ListView的OnItemClickListener詳解
這篇文章主要介紹了Android ListView的OnItemClickListener詳解的相關(guān)資料,涉及到OnItemClickListener的position和id參數(shù)做詳細(xì)的解釋的知識(shí)點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-07-07
Android內(nèi)存泄漏排查利器LeakCanary
這篇文章主要為大家詳細(xì)介紹了Android內(nèi)存泄漏排查利器LeakCanary的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android自定義布局實(shí)現(xiàn)仿qq側(cè)滑部分代碼
這篇文章主要為大家詳細(xì)介紹了自定義布局實(shí)現(xiàn)仿qq側(cè)滑Android部分代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
android實(shí)現(xiàn)圖片閃爍動(dòng)畫效果的兩種實(shí)現(xiàn)方式(實(shí)用性高)
本文通過(guò)兩種方法給大家講解了android實(shí)現(xiàn)圖片閃爍動(dòng)畫效果,實(shí)用性非常高,對(duì)這兩種方法感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-09-09

