2020/12/29 - [안드로이드] - [안드로이드] Notification 알림창에 댓글(Reply) 기능 추가
이번에는 알림창에 ProgressBar를 적용시켜서 파일 다운로드 상태를 볼 수 있게 만들어 보겠습니다.
1. 메인화면 구현(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림 호출"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 메인기능 구현(MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public final String CHANNEL_ID = "my_notification_channel";
public static final int NOTIFICATION_ID = 101;
public static final String TEXT_REPLY = "text_reply";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button notificationBtn = findViewById(R.id.button);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayNotification(v);
}
});
}
//알림 설정
public void displayNotification(View v){
createNotificationChannel();
//MainActivity 설정
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent MainPendingIntent = PendingIntent.getActivity(this , 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
//알림설정
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_get);
builder.setContentTitle("Image Download");
builder.setContentText("Download in progress");
builder.setPriority(NotificationManagerCompat.IMPORTANCE_DEFAULT);
builder.setAutoCancel(true); //알림 터치시 자동 삭제할 것인지
builder.setContentIntent(MainPendingIntent);// 알림 눌렀을 때 실행할 작업 인텐트 설정
final int max_progress = 100; //최대값
final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
Thread thread = new Thread()
{
@Override
public void run() {
int count = 0;
try {
while(count <= 100)
{
count = count+10;
sleep(1000);
//progress 설정(최대값,현재값,작업진행 표시유무)
builder.setProgress(max_progress, count, false);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
builder.setContentText("Download compleate");
builder.setProgress(0, 0, false); //초기화
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}catch (InterruptedException e){ }
}
};
thread.start();
}
//채널 셋팅
private void createNotificationChannel(){
//오레오부터는 알림을 채널에 등록해야 한다.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "채널이름";
String description = "채널설명";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
//채널생성
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
//알림매니저 생성
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//알림매니저에 채널등록록
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
이미지 추가
res -> drawable
3. 실행화면
[안드로이드] 텍스트(EditText) 입력 체크 및 버튼(Button) 활성화 (0) | 2021.01.01 |
---|---|
[안드로이드] Notification 커스텀 알림창 쉽게 적용해보기 (0) | 2020.12.31 |
[안드로이드] Notification 알림창에 댓글(Reply) 기능 추가 (0) | 2020.12.29 |
[안드로이드]Notification 알림창에 액션기능 추가하기 (2) | 2020.12.28 |
[안드로이드]Notification 간단한 알림 띄우기 (0) | 2020.12.25 |
댓글 영역