http://www.vogella.de/articles/AndroidNotifications/article.html
Create a new project "de.vogella.android.notificationmanager" with the Activity "CreateNotification". Create the following layout "result.xml".
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="This is the result activity opened from the notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1"></TextView> </LinearLayout>
Change "main.xml" to the following.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Create Notification" android:onClick="createNotification" android:id="@+id/button1" android:layout_height="match_parent" android:layout_width="match_parent"></Button> </LinearLayout>
Create a new activity "NotificationReceiver" with the following coding. Don't forget to register the activity in the "AndroidManfest.mf".
package de.vogella.android.notificationmanager; import android.app.Activity; import android.os.Bundle; public class NotificationReceiver extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); } }
Change the activity "CreateNotification" to the following.
package de.vogella.android.notificationmanager; 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; public class CreateNotification extends Activity {/** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void createNotification(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "A new notification", System.currentTimeMillis()); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "This is the title", "This is the text", activity); notification.number += 1; notificationManager.notify(0, notification); } }
Run your application and press the button. A new notification is created. If you select it your second activity will be displayed.
'Interesting > TIPTECH' 카테고리의 다른 글
2012년 1월 경남행정기관 페이스북 운영을 지켜보며 (0) | 2012.01.18 |
---|---|
넥서스S ICS 올리기 (T-Mobile) (0) | 2012.01.08 |
FaceLock for ICS 4.0.3 (0) | 2011.12.30 |
Nexus S ICS 4.0.3 (0) | 2011.12.19 |
[RADIO/GUIDE/TWEAK]Radio JK1,JK8,KB1,KB2,KB3,KD1,KF1 & UCKD1,KE1,KF1 with HSPDA Tweak (0) | 2011.12.19 |