Android實現振動效果

fmms 13年前發布 | 33K 次閱讀 Android Android開發 移動開發

Android實現振動效果看下面代碼

布局文件main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:orientation="vertical" >  

        <TextView  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="@string/hello" />  

        <Button  
            android:id="@+id/btn"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="振動測試" />  

    </LinearLayout>  
Activity文件
    package com.suxh.activity;  

    import android.app.Activity;  
    import android.app.Service;  
    import android.content.pm.ActivityInfo;  
    import android.os.Bundle;  
    import android.os.Vibrator;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.view.Window;  
    import android.view.WindowManager;  
    import android.widget.Button;  

    public class Activity1 extends Activity {  
        private Button btn;  
        private Vibrator mVibrator01;  //聲明一個振動器對象   
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            // 設置為無標題欄   
            requestWindowFeature(Window.FEATURE_NO_TITLE);  
            // 設置為全屏模式   
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
            // 設置為橫屏   
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
            setContentView(R.layout.main);  

            btn = (Button)findViewById(R.id.btn);  

            btn.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    mVibrator01 = ( Vibrator ) getApplication().getSystemService(Service.VIBRATOR_SERVICE);  
                    mVibrator01.vibrate( new long[]{100,10,100,1000},-1);  
                }  
            });  

        }  
    }  

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!