android notification的用法

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jackxlee.blog.51cto.com/2493058/682435


 Notification和NotificationManager的操作相对比较简单,一般用来获取系统级的服务NotificationManager,然后实例化Notification的对象,设置它的一系列属性(比如说图标、时间、标题、内容等),最后通过NotificationManager发出通知即可。

The description from SDK about Notification:

 
 
  1. A class that represents how a persistent notification is to be presented to the user using the NotificationManager.   
  2.  
  3. The Notification.Builder has been added to make it easier to construct Notifications.  
  4.  
  5. For a guide to creating notifications, see the Creating Status Bar Notifications document in the Dev Guide. 

接下来使用一个简单的案例来演示一下Notification的使用:

首先MainActivity的代码如下:

 
 
  1. public class MainActivity extends Activity {  
  2.     private Button btnSend;  
  3.       
  4.     //定义BroadcastReceiver的action  
  5.     private static final String NotificationDemo_Action = "com.ceo.notification.activity.NotificationDemo_Action";  
  6.       
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.           
  13.         //get the widgets' instance  
  14.         getInstance();  
  15.           
  16.         btnSend.setOnClickListener(new View.OnClickListener() {  
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 Intent intent = new Intent();  
  20.                 intent.setAction(NotificationDemo_Action);  
  21.                 sendBroadcast(intent);  
  22.             }  
  23.         });  
  24.     }  
  25.       
  26.     public void getInstance() {  
  27.         btnSend = (Button)findViewById(R.id.btnSend);  
  28.           
  29.     }  

相对应的main.xml布局:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:gravity="center" 
  11.     android:textColor="#EEE" 
  12.     android:textStyle="bold" 
  13.     android:textSize="25sp" 
  14.     android:text="Notification应用的小案例" 
  15.     /> 
  16. <Button 
  17.     android:id="@+id/btnSend" 
  18.     android:text="send notification" 
  19.     android:layout_width="wrap_content" 
  20.     android:layout_height="wrap_content" 
  21.     />      
  22. </LinearLayout> 

SecondActivity的代码如下:

 
 
  1. public class SecondActivity extends Activity {  
  2.     private Button btnCancel;  
  3.     //声明Notification  
  4.     private Notification notification;  
  5.     //声明NotificationManager  
  6.     private NotificationManager mNotification;  
  7.     //标识Notification的ID  
  8.     private static final int ID1;  
  9.       
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.second);  
  15.           
  16.         getInstance();  
  17.         //怎样获得NotificationManager的实例?  
  18.         String service = NOTIFICATION_SERVICE;  
  19.         mNotification = (NotificationManager)getSystemService(service);  
  20.           
  21.         //获得Notification的实例  
  22.         notification = new Notification();  
  23.           
  24.         //设置该图标 会在状态栏显示  
  25.         int icon = notification.icon = android.R.drawable.stat_notify_chat;  
  26.         //设置提示信息  
  27.         String tickerText = "Test Notification";  
  28.         //设置显示时间  
  29.         long when = System.currentTimeMillis();  
  30.         notification.icon = icon;  
  31.         notification.tickerText = tickerText;  
  32.         notification.when = when;  
  33.           
  34.         Intent intent = new Intent(this, MainActivity.class);  
  35.         PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);  
  36.         notification.setLatestEventInfo(this, "消息", "Hello Android", pi);  
  37.         mNotification.notify(ID, notification);  
  38.           
  39.         btnCancel.setOnClickListener(new View.OnClickListener() {  
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 mNotification.cancel(ID);  //--->取消通知  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47.     public void getInstance() {  
  48.         btnCancel = (Button)findViewById(R.id.btnCancel);  
  49.     }  
  50.       

相对应的second.xml布局:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:gravity="center" 
  11.     android:textColor="#EEE" 
  12.     android:textStyle="bold" 
  13.     android:textSize="25sp" 
  14.     android:text="显示通知界面" 
  15.     /> 
  16. <Button 
  17.     android:id="@+id/btnCancel" 
  18.     android:text="cancel notification" 
  19.     android:layout_width="wrap_content" 
  20.     android:layout_height="wrap_content" 
  21.     />      
  22. </LinearLayout> 

MyReceiver的代码如下:

 
 
  1. public class MyReceiver extends BroadcastReceiver {  
  2.  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         //实例化Intent  
  6.         Intent i = new Intent();  
  7.         //在新任务中启动Activity  
  8.         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  9.         //设置Intent启动的组件名称  
  10.         i.setClass(context, SecondActivity.class);  
  11.         //启动Activity,显示通知  
  12.         context.startActivity(i);  
  13.     }  
  14.  

当然不要忘了在AndroidManifest文件中注册广播和Activity等:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.ceo.notification.activity" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  7.         <activity android:name=".MainActivity" 
  8.                   android:label="@string/app_name"> 
  9.             <intent-filter> 
  10.                 <action android:name="android.intent.action.MAIN" /> 
  11.                 <category android:name="android.intent.category.LAUNCHER" /> 
  12.             </intent-filter> 
  13.         </activity> 
  14.     <receiver android:name="MyReceiver"> 
  15.         <intent-filter> 
  16.             <action android:name="com.ceo.notification.activity.NotificationDemo_Action"/> 
  17.         </intent-filter> 
  18.     </receiver>     
  19.     <activity android:name=".SecondActivity"></activity> 
  20.     </application> 
  21.     <uses-sdk android:minSdkVersion="8" /> 
  22.  
  23. </manifest>  

最后直接上图:

至此Android中Notification的使用介绍完毕,预祝大家成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值