일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
- 파이썬
- 인프라
- 고전역학
- 유닉스
- lombok
- chatGPT's answer
- JVM
- 소프트웨어공학
- Spring Batch
- 웹 크롤링
- kotlin
- write by chatGPT
- GPT-4's answer
- 자바
- GIT
- 코틀린
- 자바네트워크
- NIO
- 자바암호
- Spring boot
- Database
- write by GPT-4
- python
- 역학
- android
- 시스템
- 뉴턴역학
- Java
- 리눅스
- oracle
- Today
- Total
Akashic Records
Android Service에서 AlertDialog 띄우기 본문
Android Service에서 AlertDialog을 원칙적으로 띄울 수는 없다.
Dialog 형태의 Activity을 만들어 띄우는 편법을 사용해야 한다.
Step 1. AlertDialog Activity 정의, theme을 "Theme.Dialog”로 정의해서 PopUp 형태를 취할 수 있게 한다.
<activity
android:name=".AlertDialogActivity"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Step 2. AlertDialog Layout “alertdialog.xml”, 원하는 형태의 Dialog Layout을 작성한다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/notification_icon"
android:layout_centerHorizontal="true" />
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_below="@id/imgView"
android:layout_height="200px">
<TextView
android:text=""
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="@+id/submit"
android:layout_below="@id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
Step 3. AlertDialog Activity, notiMessage을 Service로 부터 전달 받게 된다. Button을 클릭하면 Dialog 창을 닫을 수 있게 Activity를 finish 해준다.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class AlertDialogActivity extends Activity {
private String notiMessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle bun = getIntent().getExtras();
notiMessage = bun.getString("notiMessage");
setContentView(R.layout.alertdialog);
TextView adMessage = (TextView)findViewById(R.id.message);
adMessage.setText(notiMessage);
Button adButton = (Button)findViewById(R.id.submit);
adButton.setOnClickListener(new SubmitOnClickListener());
}
private class SubmitOnClickListener implements onClickListener {
public void onClick(View v) {
finish();
}
}
}
Step 4. Service에서 AlertDialog 띄우기, intent 전달은 PendingIntent를 사용해야 한다.
Bundle bun = new Bundle();
bun.putString("notiMessage", text);
Intent popupIntent = new Intent(getApplicationContext(), AlertDialogActivity.class);
popupIntent.putExtras(bun);
PendingIntent pie= PendingIntent.getActivity(getApplicationContext(), 0, popupIntent, PendingIntent.FLAG_ONE_SHOT);
try {
pie.send();
} catch (CanceledException e) {
LogUtil.degug(e.getMessage());
}
'오래된글 > Articles' 카테고리의 다른 글
시스템 부팅시 도커 컨테이너 자동 실행 (0) | 2018.05.03 |
---|---|
First WebSocket (0) | 2018.05.03 |
Android 촬영 사진의 회전 각도 구하기 (0) | 2018.05.03 |
모바일 보안 취약점 및 대책 (0) | 2018.05.03 |
Database Design Best Practices (0) | 2018.05.03 |