반응형
진동 / 효과음


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_vibration"
android:text="Vibration"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_system_beep"
android:text="System beep"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_custom_beep"
android:text="Custom beep"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.tistory.jaringobi.notification;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button vibrationBtn;
Button systemBeepBtn;
Button customBeepBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vibrationBtn = (Button)findViewById(R.id.btn_vibration);
systemBeepBtn = (Button)findViewById(R.id.btn_system_beep);
customBeepBtn = (Button)findViewById(R.id.btn_custom_beep);
vibrationBtn.setOnClickListener(this);
systemBeepBtn.setOnClickListener(this);
customBeepBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view==vibrationBtn){
Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vib.vibrate(1000);
// vibrationBtn, 진동 1초
}else if (view==systemBeepBtn){
Uri notification = RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
ringtone.play();
// systemBeepBtn
}else if(view == customBeepBtn){
MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.fallbackring);
player.start();
// customBeepBtn - raw 폴더를 만들어 넣어 준 fallbackring 파일을 재생
}
}
}

Snack Bar
- 하단에 나타나는 형태
- 롤리팝 (API 21)부터 사용 가능
- 스낵바를 사용하기 위해서는 라이브러리 추가가 필요
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnSnackbar1"
android:text="스낵바"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSnackbar2"
android:text="스낵바 (Click)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.tistory.jaringobi.notificationex02;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButton();
}
private void setButton(){
findViewById(R.id.btnSnackbar1).setOnClickListener(this);
findViewById(R.id.btnSnackbar2).setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId()== R.id.btnSnackbar1){
// btnSnackbar1일 경우
Snackbar.make(view, "Snackbar show!", Snackbar.LENGTH_LONG).show();
// view에 "Snackbar show!"을 가진 스낵바가 길게 나타남
}else if(view.getId() == R.id.btnSnackbar2){
// btnSnackbar2일 경우
Snackbar.make(view, "Snackbar Show Close", Snackbar.LENGTH_INDEFINITE)
// view에 "Snackbar Show Close"를 가진 스낵바가 사라지지 않음
.setAction("닫기", new View.OnClickListener(){
// 닫기라는 버튼 설정
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"스낵바 닫기를 클릭했습니다.",
Toast.LENGTH_LONG).show();
// 닫기 메시지 누르면 토스트 메시지 출력
}
}).show();
}
}
}



반응형
'자린고비 독학하기 > 자린고비 코딩하기' 카테고리의 다른 글
C언어 basic - 11~22 (0) | 2019.08.29 |
---|---|
C언어 - basic 01-10 (0) | 2019.08.28 |
Android Studio - Viewpager (0) | 2019.08.28 |
Android Studio - FrameLayout (0) | 2019.08.22 |
Android Studio - Relative Layout (0) | 2019.08.21 |