android 之實現手機震動功能

來源:互聯網
上載者:User

標籤:

介面:利用weight屬性,能比較好得移植到平板上。

    

布局代碼:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.example.vibrator.MainActivity">

<TextView
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ToggleButton
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/text2"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ToggleButton
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/text1"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ToggleButton
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/text3"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

實現震動功能代碼:
//要注意再mainfest中添加許可權:<uses-permission android:name="android.permission.VIBRATE"/>
public class MainActivity extends AppCompatActivity {
//建立震動服務物件
private Vibrator mVibrator;
private ToggleButton toggleButton1,toggleButton2,toggleButton3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//擷取手機震動服務
mVibrator=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
toggleButton1.setOnClickListener(new myOnClickListener1());
toggleButton2.setOnClickListener(new myOnClickListener2());
toggleButton3.setOnClickListener(new myOnClickListener3());
}
private void init(){
toggleButton1=(ToggleButton)findViewById(R.id.button1);
toggleButton2=(ToggleButton)findViewById(R.id.button2);
toggleButton3=(ToggleButton)findViewById(R.id.button3);
}
private class myOnClickListener1 implements View.OnClickListener{
@Override
public void onClick(View v) {
//判斷是否開啟
if(toggleButton1.isChecked()){
//設定震動周期,數組表示時間:等待+執行,單位是毫秒,下面操作代表:等待100,執行100,等待100,執行1000,
//後面的數字如果為-1代表不重複,之執行一次,其他代表會重複,0代表從數組的第0個位置開始
mVibrator.vibrate(new long[]{100,100,100,1000},-1);
Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
}else{
//取消震動
mVibrator.cancel();
Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
}
}
}
private class myOnClickListener2 implements View.OnClickListener{
@Override
public void onClick(View v) {
if(toggleButton2.isChecked()){
mVibrator.vibrate(new long[]{100,10,100,1000},0);
Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
}else{
mVibrator.cancel();
Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
}
}
}
private class myOnClickListener3 implements View.OnClickListener{
@Override
public void onClick(View v) {
if(toggleButton3.isChecked()){
mVibrator.vibrate(new long[]{1000,50,1000,50,1000},0);
Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
}else{
mVibrator.cancel();
Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
}
}
}
}

 

android 之實現手機震動功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.