1. 程式人生 > >Android基礎(三)通訊領域2.手機震動

Android基礎(三)通訊領域2.手機震動

xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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:id="@+id/mTextview01"
        android:text="hello"/>
    <TextView
        android:layout_width="127px"
        android:layout_height="35px"
        android:layout_x="90px"
        android:layout_y="33px"
        android:id="@+id/myTextView02"/>
    <TextView
        android:layout_width="127px"
        android:layout_height="35px"
        android:layout_x="90px"
        android:layout_y="115px"
        android:id="@+id/myTextview03"/>
    <TextView
        android:layout_width="127px"
        android:layout_height="35px"
        android:layout_x="90px"
        android:layout_y="215px"
        android:id="@+id/myTextview04"/>
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myToggle01"
        android:layout_x="29px"
        android:layout_y="31px"/>
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myToggle02"
        android:layout_x="29px"
        android:layout_y="114px"/>
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myToggle03"
        android:layout_x="29px"
        android:layout_y="214px"/>
</AbsoluteLayout>

java:

import android.app.Service;
import android.bluetooth.BluetoothClass;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    private Vibrator mVibrator01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //設定ToggleButton物件
        mVibrator01=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        final ToggleButton mtogglebutton1=(ToggleButton)findViewById(R.id.myToggle01);
        final ToggleButton mtogglebutton2=(ToggleButton)findViewById(R.id.myToggle02);
        final ToggleButton mtogglebutton3=(ToggleButton)findViewById(R.id.myToggle03);
        mtogglebutton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mtogglebutton1.isChecked())
                {
                    //設定震動週期
                    mVibrator01.vibrate(new long[]{100,10,100,1000},-1);
                    //同Toast顯示振動啟動
                    Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    //取消震動
                    mVibrator01.cancel();
                    Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
                }
            }
        });
        mtogglebutton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mtogglebutton2.isChecked())
                {
                    //設定震動週期
                    mVibrator01.vibrate(new long[]{100,100,100,1000},0);
                    //同Toast顯示振動啟動
                    Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    //取消震動
                    mVibrator01.cancel();
                    Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
                }
            }
        });
        mtogglebutton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mtogglebutton3.isChecked())
                {
                    //設定震動週期
                    mVibrator01.vibrate(new long[]{1000,50,1000,50},0);
                    //同Toast顯示振動啟動
                    Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    //取消震動
                    mVibrator01.cancel();
                    Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

1.Vibrator 震動器類 2.Toast 提醒框