프로그래밍/안드로이드

안드로이드 스마트폰 진동을 내맘대로 다뤄보자

가카리 2012. 8. 3. 19:30
반응형

Object Activity.getSystemService(String name) 으로 서비스를 받아온뒤

void vibrate(long milliseconds)

void vibrate(long[] pattern, int repeat)

void cancel()

을 이용합니다.

메니페스트파일에 퍼미션 추가에 유의합시다.

메니페스트 파일 접근 방법은 아래에 있습니다.

 

 

 

 

 

 

 

 

package com.android.ex26;

 

import android.app.Activity;

import android.content.Context;

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 ex26 extends Activity {

       Vibrator mVib;

       Button btn1;

       Button btn2;

       Button btn3;

      

       /** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

 

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

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

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

 

btn1.setOnClickListener(mListener);

btn2.setOnClickListener(mListener);

btn3.setOnClickListener(mListener);

 

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

}

public void onDestroy(){

       super.onDestroy();

       mVib.cancel();

}

 

OnClickListener mListener = new OnClickListener() {

            

             @Override

             public void onClick(View v) {

                    // TODO Auto-generated method stub

                    switch(v.getId()){

              case R.id.btn1:

                     mVib.vibrate(500);

                     Toast.makeText(ex26.this, "첫번째버튼", Toast.LENGTH_SHORT).show();

                     break;

              case R.id.btn2:

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

                     Toast.makeText(ex26.this, "두번째버튼", Toast.LENGTH_SHORT).show();

                     break;

              case R.id.btn3:

                     mVib.cancel();

                     break;

             

              }

             }

       };

 

}

 

 

 

유의 할점 반드시 퍼미션에 아래와같이

<uses-permission android:name="android.permission.VIBRATE"/> 추가해줘야됨

 

<?xml version="1.0" encoding="utf-8"?>

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

package="com.android.ex26"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="7" />

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

 

<application android:icon="@drawable/icon"        

              android:label="@string/app_name">

 

<activity android:name=".ex26"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

 

</application>

</manifest>

  

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> 

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="버튼을 누르면 진동합니다"

/>

 

<Button

      android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="한번 진동"

android:id="@+id/btn1"

/>

<Button

       android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="계속 진동"

android:id="@+id/btn2"

/>

<Button

       android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 android:text="진동 중지"

 android:id="@+id/btn3"

/>

</LinearLayout>