经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 状态栏通知 Notification
来源:cnblogs  作者:thomas_blog  时间:2019/11/6 8:34:18  对本文有异议
  1. private NotificationManager manager;
  2. private Notification.Builder builder;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. button = (Button) findViewById(R.id.button);
  8. // 创建一个通知管理类
  9. manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  10. builder = new Notification.Builder(this);
  11. button.setOnClickListener(new OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. // TODO Auto-generated method stub
  15. Intent intent = new Intent(MainActivity.this, MainActivity.class);
  16. PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
  17. builder.setContentIntent(pendingIntent);
  18. builder.setContentTitle("new Notification here");
  19. builder.setContentText("hello furong");
  20. builder.setSmallIcon(R.drawable.a1);
  21. builder.setTicker("有通知来了"); // 第一次出现在状态栏
  22. // 需要用户授权 android.permission.VIBRATE
  23. builder.setDefaults(Notification.DEFAULT_ALL); // 所有的提示都默认
  24. Notification notification = builder.build();
  25. manager.notify(1000, notification);
  26. }
  27. });
  28. }

添加用户权限

image

试验效果

image

image

原文链接:http://www.cnblogs.com/zhangxuechao/p/11801278.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号