Android實(shí)現(xiàn)簡(jiǎn)單手機(jī)震動(dòng)效果
手機(jī)開(kāi)發(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的元素開(kāi)始重復(fù).
SystemClock.sleep(10000);//震動(dòng)10s以后停止
vibrator.cancel();
break;
default:
break;
}
}
};
}
5.運(yùn)行效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動(dòng)畫(huà)控件
這篇文章主要介紹了Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動(dòng)畫(huà)控件,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04
Android實(shí)現(xiàn)3D標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
21天學(xué)習(xí)android開(kāi)發(fā)教程之SurfaceView
21天學(xué)習(xí)android開(kāi)發(fā)教程之SurfaceView,SurfaceView由于可以直接從內(nèi)存或者DMA等硬件接口取得圖像數(shù)據(jù),因此是個(gè)非常重要的繪圖容器,操作相對(duì)簡(jiǎn)單,感興趣的小伙伴們可以參考一下2016-02-02
Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié))
這篇文章主要介紹了Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android來(lái)電攔截的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android來(lái)電攔截的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法,主要通過(guò)繼承Animation自定義Rotate3D來(lái)實(shí)現(xiàn)3D翻頁(yè)效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
android 退出程序解決內(nèi)存釋放的問(wèn)題
做Android項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)問(wèn)題:當(dāng)應(yīng)用程序退出了,點(diǎn)擊"設(shè)置"查看應(yīng)用程序,界面顯示著可以點(diǎn)擊"強(qiáng)制關(guān)閉 由于這個(gè)問(wèn)題我發(fā)現(xiàn)了一個(gè)更加嚴(yán)重的問(wèn)題,那就是,在我應(yīng)用程序退出之后,系統(tǒng)并沒(méi)有釋放掉我應(yīng)用程序所占內(nèi)存2012-11-11
Android App中使用Gallery制作幻燈片播放效果
這篇文章主要介紹了Android App中使用Gallery制作幻燈片播放效果,相冊(cè)應(yīng)用中的輪播功能也與本文中例子的原理類似,需要的朋友可以參考下2016-04-04

