android之notifiation的例子

通知用于在顶部显示类似ios的push通知之类的,下面是sample代码

 1 package com.example.test;
 2 
 3 import android.app.Activity;
 4 import android.app.Notification;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Context;
 8 import android.content.Intent;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 
14 public class MainActivity extends Activity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         
21         Button testBtn = (Button)findViewById(R.id.button1);
22         testBtn.setOnClickListener(new OnClickListener() {
23             
24             @Override
25             public void onClick(View v) {
26                 // TODO Auto-generated method stub
27                 
28                 addNotificaction();
29             }
30         });
31     }
32 
33     @SuppressWarnings("deprecation")
34     protected void addNotificaction() {
35         // TODO Auto-generated method stub
36         NotificationManager manager = (NotificationManager) this  
37                 .getSystemService(Context.NOTIFICATION_SERVICE);  
38                 // 创建一个Notification  
39                 Notification notification = new Notification();  
40                 // 设置显示在手机最上边的状态栏的图标  
41                 notification.icon = R.drawable.ic_launcher;  
42                 // 当当前的notification被放到状态栏上的时候,提示内容  
43                 notification.tickerText = "注意了,我被扔到状态栏了";  
44                   
45                 /*** 
46                  * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发 
47                  * notification.contentView:我们可以不在状态栏放图标而是放一个view 
48                  * notification.deleteIntent 当当前notification被移除时执行的intent 
49                  * notification.vibrate 当手机震动时,震动周期设置 
50                  */  
51                 // 添加声音提示  
52                 notification.defaults=Notification.DEFAULT_SOUND;  
53                 // audioStreamType的值必须AudioManager中的值,代表着响铃的模式  
54                 notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
55                   
56                 //下边的两个方式可以添加音乐  
57                 //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");   
58                 //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");   
59                 Intent intent = new Intent(this, MainActivity.class);  
60                 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
61                 // 点击状态栏的图标出现的提示信息设置  
62                 notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);  
63 
64                 manager.notify(1, notification);  
65     }
66 }

 

posted @ 2013-05-25 09:35  自由出土文物  阅读(275)  评论(0编辑  收藏  举报