Chap6. 인터페이스 기초03 - Custom View, Toast, Beep, Vibration, etc
패키지명 : Exam01
//onClick 메소드
case R.id.btnToast:
Toast.makeText(this, "SEIN", Toast.LENGTH_SHORT).show();
break;
//onClick 메소드
case R.id.btnSound:
SoundPool soundPool = new SoundPool(1, STREAM_MUSIC, 0);
int sound = soundPool.load(this, R.raw.dingdong, 1);
soundPool.play(sound, 1, 1, 0, 0, 1);
break;
//onClick 메소드
case R.id.btnVibration:
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
break;
심화) 다양한 진동 패턴으로 변경시켜 볼 것
//onClick 메소드
case R.id.btnVibration:
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(new long[] {100, 50, 200, 50}, 0);
break;
//짝수 인덱스 = 대기시간, 홀수 인덱스 = 진동 시간
//Manifest에 퍼미션 부여 잊지 말기
<uses-permission android:name="android.permission.VIBRATE" />
package ddwucom.mobile.test05;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Toast;
import static android.media.AudioManager.STREAM_MUSIC;
public class MainActivity extends AppCompatActivity {
SoundPool soundPool;
int sound;
Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
soundPool = new SoundPool(1, STREAM_MUSIC, 0);
sound = soundPool.load(this, R.raw.dingdong, 1);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnToast:
Toast.makeText(this, "SEIN", Toast.LENGTH_SHORT).show();
break;
case R.id.btnSound:
soundPool.play(sound, 1, 1, 0, 0, 1);
break;
case R.id.btnVibration:
vibrator.vibrate(1000);
vibrator.vibrate(new long[] {100, 50, 200, 50}, 0);
break;
}
}
}
-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnToast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="TOAST" />
<Button
android:id="@+id/btnSound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="SOUND" />
<Button
android:id="@+id/btnVibration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="VIBRATION" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ddwucom.mobile.test05">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>