1 public class LaunchNotificationActivity
 2     extends Activity {
 3     /** Called when the activity is first created. */
 4     @Override
 5     public void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7 
 8         LinearLayout layout = new LinearLayout(this);
 9         layout.setOrientation(LinearLayout.VERTICAL);
10 
11         TextView textView = new TextView(this);
12         textView.setText("演示生成通知。");
13 
14         Button button = new Button(this);
15         button.setText("通知演示");
16         button.setOnClickListener(new OnClickListener() {
17             @Override
18             public void onClick(View v) {
19                 NotificationManager mNotificationManager =
20                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
21                 int icon = R.drawable.smalllogo;
22                 long when = System.currentTimeMillis();
23                 Notification notification = new Notification(icon, "掌中彩温馨提示", when);// 第一个参数为图标,第二个参数为标题,第三个为通知时间
24                 notification.defaults = Notification.DEFAULT_SOUND;// 发出默认声音
25 // Intent openintent = new Intent(this, OtherActivity.class);
26                 PendingIntent contentIntent =
27                     PendingIntent.getActivity(LaunchNotificationActivity.this, 0, null, 0);// 当点击消息时就会向系统发送openintent意图
28                 notification.setLatestEventInfo(LaunchNotificationActivity.this, "标题",
29                                                 "亲~您已经一周没打XXX了,随机一注试试手气吧", contentIntent);
30                 notification.contentIntent =
31                     PendingIntent.getActivity(LaunchNotificationActivity.this, 0,
32                                               new Intent(LaunchNotificationActivity.this,
33                                                          LaunchNotificationActivity.class), 0);
34                 notification.flags |= Notification.FLAG_AUTO_CANCEL;// 选择后不再在通知栏显示
35                 notification.defaults |= Notification.DEFAULT_SOUND;
36                 mNotificationManager.notify(0, notification);// 发送通知
37             }
38         });
39 
40         layout.addView(textView);
41         layout.addView(button);
42         this.setContentView(layout);
43     }
44 }