你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中Notification的用法

[日期:2012-02-22] 来源:Linux社区  作者:cjjky [字体:大 中 小]
[5] SecondActivity.java源码如下:

[html]
  1. package com.andyidea.notification;  
  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.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11.   
  12. public class SecondActivity extends Activity {  
  13.   
  14.     private Button btnCancel;    
  15.     //声明Notification    
  16.     private Notification notification;    
  17.     //声明NotificationManager    
  18.     private NotificationManager mNotification;    
  19.     //标识Notification的ID    
  20.     private static final int ID = 1;    
  21.         
  22.     @Override    
  23.     protected void onCreate(Bundle savedInstanceState) {    
  24.         super.onCreate(savedInstanceState);    
  25.         setContentView(R.layout.secondlayout);    
  26.             
  27.         btnCancel = (Button)findViewById(R.id.btnCancel);   
  28.         //怎样获得NotificationManager的实例?    
  29.         String service = NOTIFICATION_SERVICE;    
  30.         mNotification = (NotificationManager)getSystemService(service);    
  31.             
  32.         //获得Notification的实例    
  33.         notification = new Notification();    
  34.             
  35.         //设置该图标 会在状态栏显示    
  36.         int icon = notification.icon = android.R.drawable.stat_sys_phone_call;    
  37.         //设置提示信息    
  38.         String tickerText = "Test Notification";    
  39.         //设置显示时间    
  40.         long when = System.currentTimeMillis();    
  41.         notification.icon = icon;    
  42.         notification.tickerText = tickerText;    
  43.         notification.when = when;    
  44.             
  45.         Intent intent = new Intent(this, MainActivity.class);    
  46.         PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);    
  47.         notification.setLatestEventInfo(this, "消息", "SMS Android", pi);    
  48.         mNotification.notify(ID, notification);    
  49.             
  50.         btnCancel.setOnClickListener(new View.OnClickListener() {    
  51.             @Override    
  52.             public void onClick(View v) {    
  53.                 mNotification.cancel(ID);  //--->取消通知    
  54.             }    
  55.         });    
  56.     }    
  57.         
  58. }  
[6] NotificationReceiver.java源码如下:

[html]
  1. package com.andyidea.notification;  
  2.   
  3. import com.andyidea.notification.SecondActivity;  
  4.   
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8.   
  9. public class NotificationReceiver extends BroadcastReceiver {  
  10.   
  11.     @Override  
  12.     public void onReceive(Context context, Intent intent) {  
  13.         //实例化Intent    
  14.         Intent i = new Intent();    
  15.         //在新任务中启动Activity    
  16.         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
  17.         //设置Intent启动的组件名称    
  18.         i.setClass(context, SecondActivity.class);    
  19.         //启动Activity,显示通知    
  20.         context.startActivity(i);    
  21.     }  
  22.   
  23. }  
[7]程序运行效果如下:





linux
相关资讯       Android开发 
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* suo 发表于 2012/2/23 18:05:03
我是新手 很想知道我为什么运行不了 我QQ 261428669 求高手指点
第 1 楼
* suo 发表于 2012/2/23 18:03:42
为什么 我照着你的代码写 运行以后点 send notification 却没有任何响应呢
能帮帮我吗