본문 바로가기

Android

출력 - 진동(vibrate)

Activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/btnvibrate1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="한번 진동" />

   

    <Button

        android:id="@+id/btnvibrate2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="계속 진동" />

   

    <Button

        android:id="@+id/btnvibratestop"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="진동 중지" />

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.testex17;

 

import android.os.Bundle;

 

import android.os.Vibrator;

import android.app.Activity;

import android.content.Context;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends Activity {

     Vibrator vibrator;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

 

           Button btn1 = (Button) findViewById(R.id.btnvibrate1);

           Button btn2 = (Button) findViewById(R.id.btnvibrate2);

           Button btn3 = (Button) findViewById(R.id.btnvibratestop);

 

           btn1.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     vibrator.vibrate(500);

                }

           });

 

           btn2.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     vibrator.vibrate(new long[] { 100, 50, 200, 50 }, 0);

                }

           });

 

           btn3.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     vibrator.cancel();

                }

           });

     }

 

    

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 

AndroidMainfest.xml에 다음의 코드를 추가한다.

 <uses-permission android:name="android.permission.VIBRATE"/>