일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 시놀로지
- AC1300
- jQuery
- readonly
- 크로스 컴파일
- replace
- javascript
- Getrows
- euc-kr
- form
- XMLHTTP
- ASP
- DS718+
- submit
- disabled
- Injection
- RTL88x2BU
- include
- lsusb
- 따라다니는 배너
- ajax
- println
- jquery 레이어
- 무선랜카드
- synology
- SQL Injection
- FileSystemObject
- utf-8
- PHP GCM
- 커널
- Today
- Total
아는 것이 좋은 것이다.
[Android] 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());
}
'Android' 카테고리의 다른 글
[Android] actionbar color 변경 (0) | 2014.10.16 |
---|---|
[Android] 안드로이드 타이틀바 제거 (0) | 2014.07.23 |
[Android] Webview 속도개선, 성능개선 (0) | 2014.07.21 |
[Andorid] webview 기본세팅 (0) | 2014.07.20 |
[Andorid] 백버튼 2번 터치시 앱 종료하기 (0) | 2014.06.29 |