티스토리 뷰

 

 

 

[카테고리] 제목[Android] Android Service에서 AlertDialog 호출

 

Android Service에서는 AlertDialog를 직접적으로 호출 할 수 없으므로
Dialog 형태의 Activity를 만들어서 띄우는 방식을 사용 해야 한다.

1. Menifest 설정: PopUp 형태로 호출 되도록 Theme.Dialog 설정 추가

<activity
android:name=".AlertDialogActivity"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

 

2. AlertDIalog Activity, notiMessage을 Service로 부터 전달 받게 된다. Button을 클릭하면 Dialog 창을 닫을 수 있게 Activity를 finish 해준다.

<?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>

 

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();

  }
}
}

 

 4. Service에서 AlertDialog 띄우기, intent 전달은 PendingIntent를 사용해야 한다.

Bundle mBundle = new Bundle();
mBundle.putString("notiMessage", text);

Intent popupIntent = new Intent(getApplicationContext(), AlertDialogActivity.class);

popupIntent.putExtras(mBundle);
PendingIntent pie= PendingIntent.getActivity(getApplicationContext(), 0, popupIntent, PendingIntent.FLAG_ONE_SHOT);
try {
pie.send();
} catch (CanceledException e) {
LogUtil.degug(e.getMessage());
}

댓글

파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음



Total
Today
Yesterday
최근에 달린 댓글