歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現振動效果

Android實現振動效果

日期:2017/3/1 10:27:20   编辑:Linux編程

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

布局文件main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/btn"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="振動測試" />
  15. </LinearLayout>
Activity文件
  1. package com.suxh.activity;
  2. import android.app.Activity;
  3. import android.app.Service;
  4. import android.content.pm.ActivityInfo;
  5. import android.os.Bundle;
  6. import android.os.Vibrator;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import android.widget.Button;
  12. public class Activity1 extends Activity {
  13. private Button btn;
  14. private Vibrator mVibrator01; //聲明一個振動器對象
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. // 設置為無標題欄
  19. requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. // 設置為全屏模式
  21. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  22. // 設置為橫屏
  23. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  24. setContentView(R.layout.main);
  25. btn = (Button)findViewById(R.id.btn);
  26. btn.setOnClickListener(new OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. mVibrator01 = ( Vibrator ) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
  30. mVibrator01.vibrate( new long[]{100,10,100,1000},-1);
  31. }
  32. });
  33. }
  34. }
Copyright © Linux教程網 All Rights Reserved