Android 发送通知

实现代码如下,代码中有详细注释:

[java]  view plain copy print ?
  1. public class MainActivity extends Activity {  
  2.     private TextView tvTitle;  
  3.     private TextView tvContent;  
  4.     private Button btnSend;  
  5.     private String title;  
  6.     private String content;  
  7.       
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.           
  12.         tvTitle=(TextView) findViewById(R.id.etTitle);  
  13.         tvContent=(TextView) findViewById(R.id.etContent);  
  14.         btnSend=(Button) findViewById(R.id.btnSend);  
  15.   
  16.         btnSend.setOnClickListener(new OnClickListener(){  
  17.             public void onClick(View v) {  
  18.                 send();  
  19.             }  
  20.         });  
  21.           
  22.     }  
  23.     public void send(){  
  24.         title=tvTitle.getText().toString();//标题  
  25.         content=tvContent.getText().toString();//内容  
  26.           
  27.         //1.得到NotificationManager  
  28.         NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  29.         //2.实例化一个通知,指定图标、概要、时间  
  30.         Notification n=new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());  
  31.         //3.指定通知的标题、内容和intent  
  32.         Intent intent = new Intent(this, MainActivity.class);  
  33.         PendingIntent pi= PendingIntent.getActivity(this0, intent, 0);  
  34.         n.setLatestEventInfo(this, title, content, pi);  
  35.         //指定声音  
  36.         //n.defaults = Notification.DEFAULT_SOUND;   
  37.         //4.发送通知  
  38.         nm.notify(1, n);  
  39.     }  
  40.   
  41. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值