Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android提供的系統服務之--Vibrator(振動器)

Android提供的系統服務之--Vibrator(振動器)

編輯:關於Android編程

Android提供的系統服務之--Vibrator(振動器)

——轉載請注明出處:coder-pig



Vibrator簡介與相關方法:


\



<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+vPK1pWRlbW+hqqGqyejWw8a1wso8L3N0cm9uZz48c3Ryb25nPrK7zay1xNXxtq/G9zwvc3Ryb25nPjwvcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+ttTT2lZpYnJhdG9y08O1xNfuueO3urXExKq5/dPay/nOvbXEyta7+rC0xKbG98DgtcRhcHAs1NphcHDK0LOh0rvL0SzSu7bRLLHK1d/L5rHjz8LBy7y4uPbPwsC0s/Kz8jwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz4strzKx7TzzazQodLstcQs1eK149ChzebS4jwvc3Ryb25nPjxzdHJvbmc+vrnIu9PQOFe24LXEz8LU2MG/Li4uusOwySy6w8/x0rKyu8vjtuAssru5/cbVsem5psTctrzKx8fQu7vV8bavxrXCysC0zeqzyTwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz7L+c69tcSwtMSm0Ke5+yzKx7fx1ea1xNPQ0Ke+zbK7tcO2+NaqwcssxMfDtL3Tz8LAtDwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz7O0sPHvs3AtMq1z9bSu7j2vPK1pbXEsLTEpsb3sMkhPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPrrL0MTG5Mq1vs3Kx3ZpYnJhdGUoKdbQtcTK/dfptcSyzsr9LLj5vt3X1Ly60OjH89C00ru49sr91+m+zb/J0tTByyE8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+0vLOqsSjxOLG97K7u+HV8bavtcQsy/nS1NDo0qrU2srWu/rJz9TL0NCyxbvh09DQp7n7xbYhPC9zdHJvbmc+PC9wPgo8cD48YnI+CjwvcD4KPHA+PHN0cm9uZz7Qp7n7zbw6PC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20141107/20141107083938154.jpg" alt="\">

代碼也很簡單,布局的話就四個簡單的按鈕而已

activity_main.xml:



    

接著就是MainActivity的編寫了,這裡和上一節的寫法是一樣的,讓Activity類實現OnClickListener接口

重寫onClick方法,根據不同的view的id就可以判斷是哪個按鈕點擊了,這種寫法的好處是對於組件較多的

情況這樣寫可以簡化代碼!

MainActivity.java:

package com.jay.example.vibratordemo;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener{

	private Button btnshort;
	private Button btnlong;
	private Button btnrhythm;
	private Button btncancel;
	private Vibrator myVibrator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnshort = (Button) findViewById(R.id.btnshort);
		btnlong = (Button) findViewById(R.id.btnlong);
		btnrhythm = (Button) findViewById(R.id.btnrhythm);
		btncancel = (Button) findViewById(R.id.btncancle);
		
		btnshort.setOnClickListener(this);
		btnlong.setOnClickListener(this);
		btnrhythm.setOnClickListener(this);
		btncancel.setOnClickListener(this);
		
		
		//獲得系統的Vibrator實例:
		myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnshort:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{100,200,100,200}, 0);
			Toast.makeText(getApplicationContext(), "短振動", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnlong:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{100,100,100,1000}, 0);
			Toast.makeText(getApplicationContext(), "長振動", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnrhythm:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{500,100,500,100,500,100}, 0);
			Toast.makeText(getApplicationContext(), "節奏振動", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btncancle:
			myVibrator.cancel();
			Toast.makeText(getApplicationContext(), "取消振動", Toast.LENGTH_SHORT).show();
		}
		
	}
}

最後別忘了在AndroidManifest.xml文件中添加權限哦!




好了,基本用法其實也是很簡單的,這裡就不多說了,另外上面也說了,虛擬機是沒有震動效果的,所以

需要把應用發布到手機上才能檢測效果了!參考代碼下載:vibratorDemo.rar

為了方便各位,直接把apk導出來吧,直接下載安裝到手機就可以測試效果了,當然只是個小demo,不會

推送廣告,亂發短信什麼的=-=!安裝時可以查看所需要的權限!apk下載:vibratorDemo.apk










  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved