震動程式碼:
import android.os.Bundle;
import android.os.Vibrator; // 記得匯入震動類別
import android.app.Activity;
import android.app.Service;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVibrate(1000); // 震動 1 秒
}
public void setVibrate(int time){
Vibrator myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
myVibrator.vibrate(time);
}
}
別忘了在 AndroidManifest.xml 裡的 <manifest> 加入 "震動"權限:
<uses-permission android:name="android.permission.VIBRATE" />
全站熱搜
留言列表