<?xml version="1.0" encoding="utf-8"?>
<!-- main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/listSeparatorTextViewStyle"
android:text="TickerText" android:layout_marginBottom="3dp"/>
<!--style="?android:attr/listSeparatorTextViewStyle"
TextView로 ListView의 Category를 묶는 스타일
-->
<EditText android:layout_height="wrap_content"
android:id="@+id/tickerText"
android:hint="Ticker Text"
android:layout_width="fill_parent"></EditText>
<TextView android:layout_height="wrap_content"
android:text="Notification Panel"
android:layout_width="fill_parent"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_marginBottom="3dp"></TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/contentTitle"
android:hint="Content TItle"></EditText>
<EditText android:layout_height="wrap_content"
android:id="@+id/contentText"
android:hint="Content Text"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:text="Register Notification to Notification Manager"
android:id="@+id/registerNotification"
android:layout_width="fill_parent"></Button>
</LinearLayout>
|
<?xml version="1.0" encoding="utf-8"?>
<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" package="com.androidhuman.example.NotificationBuilder">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NotificationBuilder"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="NotificationMessage"></activity>
</application>
<uses-sdk android:minSdkVersion="8"/>
</manifest>
|
// NotificationBuilder.java
package com.androidhuman.example.NotificationBuilder;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class NotificationBuilder extends Activity implements OnClickListener{
private EditText tickerText;
private EditText contentTitle;
private EditText contentText;
private Button registerButton;
private NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tickerText = (EditText)findViewById(R.id.tickerText);
contentTitle = (EditText)findViewById(R.id.contentTitle);
contentText = (EditText)findViewById(R.id.contentText);
registerButton = (Button)findViewById(R.id.registerNotification);
registerButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.registerNotification:
// Notification을 등록하기 위해 NotificationManager 객체 생성.
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent = PendingIntent.getActivity(
NotificationBuilder.this, 0,
new Intent(NotificationBuilder.this, NotificationMessage.class), 0);
// public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
// Notification List에서 항목을 선택했을 경우 NotificationMessage Activity가 실행되도록 설정.
// Activity를 호출하기 위해 getActivity()를 사용하였으나 서비스를 호출하거나 BroadCast MSG를 보내는것도 가능함.
String ticker = tickerText.getText().toString();
String title = contentTitle.getText().toString();
String text = contentText.getText().toString();
//public Notification (int icon, CharSequence tickerText, long when)
Notification notification =
new Notification(android.R.drawable.ic_dialog_email,
ticker, System.currentTimeMillis());
// android.R.drawable.에서 다양한 icon을 선택할 수 있다. R.drawable.으로 그림도 넣을수 있다.
// when에는 현재시간을 넣어 줌으로써 Notification이 Notification Manager에 등록되자마자
// 상태 표시줄에 해당 Nofitication이 표시된다.
notification.setLatestEventInfo(NotificationBuilder.this,
title, text, intent);
// Notification List에 표시될 내용을 설정한다.
// 제목 및 내용, 항목을 클릭했을경우 수행할 PendingIntent를 넣어 줌으로써 NotificationMessage Acitivity가 보여진다.
// 마지막으로 Notification을 Notification Manager에 등록함. 고유 ID 1234사용.
// public void notify (int id, Notification notification)
nm.notify(1234, notification);
// Toast Popup 출력
Toast.makeText(NotificationBuilder.this, "Notification Registered.",
Toast.LENGTH_SHORT).show();
break;
}
}
}
|
// NotificationMessage.java
package com.androidhuman.example.NotificationBuilder;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;
public class NotificationMessage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("위의 Notification이 사라진 것을 확인하셨나요? :)");
setContentView(tv);
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Notification등록시 사용한 고유ID 1234를 사용하여 Notification 해제.
nm.cancel(1234);
}
}
|