之前网上看了下自己定义消息栏,通知栏,了解到了Notification这个控件。发现UC浏览器等都是这样的类型,今天写个demo实现下。如图:



当中每一个button都有不同的功能。代码例如以下:

  1. package com.example.textwsjdemo;
  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.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.os.Bundle;
  12. import android.view.KeyEvent;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.RemoteViews;
  17. import android.widget.Toast;
  18.  
  19. public class MainActivity extends Activity {
  20.  
  21. private Button bt_hehe;
  22. private NotificationManager notificationManager;
  23. private Notification notification;
  24. private int icon;
  25. private CharSequence tickerText;
  26. private long when;
  27. RemoteViews contentView;
  28. private Intent intent;
  29. private PendingIntent pendingIntent;
  30. private int notification_id = 0;
  31. private MyBroadCast receiver;
  32. private static String ACTION = "a";
  33.  
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. receiver = new MyBroadCast();
  39. IntentFilter filter = new IntentFilter();
  40. filter.addAction("a");
  41. filter.addAction("b");
  42. filter.addAction("c");
  43. filter.addAction("d");
  44. registerReceiver(receiver, filter);
  45.  
  46. initView();
  47. initData();
  48.  
  49. }
  50.  
  51. private void initData() {
  52. icon = R.drawable.ic_launcher; // 通知图标
  53. tickerText = "Hello"; // 状态栏显示的通知文本提示
  54. when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示
  55. }
  56.  
  57. private void initView() {
  58. bt_hehe = (Button) findViewById(R.id.bt_hehe);
  59. bt_hehe.setOnClickListener(new OnClickListener() {
  60.  
  61. @Override
  62. public void onClick(View v) {
  63. // TODO Auto-generated method stub
  64. // 启动提示栏
  65. createNotification();
  66. }
  67. });
  68. }
  69.  
  70. private void createNotification() {
  71. notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  72. notification = new Notification();
  73. notification.icon = icon;
  74. notification.tickerText = tickerText;
  75. notification.when = when;
  76.  
  77. /***
  78. * 在这里我们用自定的view来显示Notification
  79. */
  80. contentView = new RemoteViews(getPackageName(),
  81. R.layout.notification_item);
  82. contentView.setTextViewText(R.id.text11, "小说");
  83. contentView.setTextViewText(R.id.text22, "视频");
  84. contentView.setTextViewText(R.id.text33, "新闻");
  85. contentView.setTextViewText(R.id.text44, "扯淡");
  86. // contentView.setTextViewText(R.id.notificationPercent, "0%");
  87. // contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);
  88. // //进度条
  89. // contentView.setImageViewResource(R.id.image,R.drawable.more_advice);
  90. // //载入图片
  91. // contentView.setImageViewResource(R.id.image,R.drawable.more_attention);
  92. // contentView.setImageViewResource(R.id.image,R.drawable.more_evaluate);
  93. // contentView.setImageViewResource(R.id.image,R.drawable.more_about);
  94. // contentView.setTextViewText(R.id.text,"Hello,this message is in a custom expanded view");
  95. // //文本
  96.  
  97. notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻。不能滑动取消
  98. //默认跳转的主界面
  99. intent = new Intent(this, MainActivity.class);
  100. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  101. pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  102.  
  103. //自己定义跳转
  104. contentView.setOnClickPendingIntent(R.id.ll_11, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("a"), PendingIntent.FLAG_UPDATE_CURRENT));
  105. contentView.setOnClickPendingIntent(R.id.ll_22, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("b"), PendingIntent.FLAG_UPDATE_CURRENT));
  106. contentView.setOnClickPendingIntent(R.id.ll_33, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("c"), PendingIntent.FLAG_UPDATE_CURRENT));
  107. contentView.setOnClickPendingIntent(R.id.ll_44, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("d"), PendingIntent.FLAG_UPDATE_CURRENT));
  108. notification.contentView = contentView;
  109. notification.contentIntent = pendingIntent;
  110. notificationManager.notify(notification_id, notification);
  111. }
  112.  
  113. // 取消通知
  114. private void cancelNotification() {
  115. notificationManager.cancelAll();
  116. }
  117.  
  118. @Override
  119. protected void onDestroy() {
  120. cancelNotification();
  121. unregisterReceiver(receiver);
  122.  
  123. }
  124.  
  125. @Override
  126. public boolean onKeyDown(int keyCode, KeyEvent event) {
  127. if ((keyCode == KeyEvent.KEYCODE_BACK)) {
  128. System.out.println("按下了back键 onKeyDown()");
  129. cancelNotification();
  130. }
  131. return super.onKeyDown(keyCode, event);
  132. }
  133.  
  134. class MyBroadCast extends BroadcastReceiver {
  135.  
  136. @Override
  137. public void onReceive(Context context, Intent intent) {
  138. if(intent.getAction().equals("a")){
  139. Toast.makeText(MainActivity.this, "11111111111111",
  140. Toast.LENGTH_LONG).show();
  141. startActivity(new Intent(MainActivity.this, ActivityText1.class));
  142. }
  143. if(intent.getAction().equals("b")){
  144. Toast.makeText(MainActivity.this, "222222222222222",
  145. Toast.LENGTH_LONG).show();
  146. startActivity(new Intent(MainActivity.this, ActivityText2.class));
  147. }
  148. if(intent.getAction().equals("c")){
  149. Toast.makeText(MainActivity.this, "333333333333",
  150. Toast.LENGTH_LONG).show();
  151. startActivity(new Intent(MainActivity.this, ActivityText3.class));
  152. }
  153. if(intent.getAction().equals("d")){
  154. Toast.makeText(MainActivity.this, "4444444444444",
  155. Toast.LENGTH_LONG).show();
  156. startActivity(new Intent(MainActivity.this, ActivityText4.class));
  157. }
  158.  
  159. }
  160.  
  161. }
  162.  
  163. }

下面是一些属性的设置:

  1. /*
  2. * 加入声音
  3. * notification.defaults |=Notification.DEFAULT_SOUND;
  4. * 或者使用下面几种方式
  5. * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
  6. * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
  7. * 假设想要让声音持续反复直到用户对通知做出反应,则能够在notification的flags字段添加"FLAG_INSISTENT"
  8. * 假设notification的defaults字段包含了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
  9. */
  10. /*
  11. * 加入振动
  12. * notification.defaults |= Notification.DEFAULT_VIBRATE;
  13. * 或者能够定义自己的振动模式:
  14. * long[] vibrate = {0,100,200,300}; //0毫秒后開始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
  15. * notification.vibrate = vibrate;
  16. * long数组能够定义成想要的不论什么长度
  17. * 假设notification的defaults字段包含了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动
  18. */
  19. /*
  20. * 加入LED灯提醒
  21. * notification.defaults |= Notification.DEFAULT_LIGHTS;
  22. * 或者能够自己的LED提醒模式:
  23. * notification.ledARGB = 0xff00ff00;
  24. * notification.ledOnMS = 300; //亮的时间
  25. * notification.ledOffMS = 1000; //灭的时间
  26. * notification.flags |= Notification.FLAG_SHOW_LIGHTS;
  27. */
  28. /*
  29. * 很多其它的特征属性
  30. * notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自己主动清除此通知
  31. * notification.flags |= FLAG_INSISTENT; //反复发出声音,直到用户响应此通知
  32. * notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在执行"组中
  33. * notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后。此通知不清除,
  34. * //常常与FLAG_ONGOING_EVENT一起使用
  35. * notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部
  36. * //假设要使用此字段,必须从1開始
  37. * notification.iconLevel = ; //
  38. */

源码下载:点击下载



Android高仿UC浏览器和360手机卫士消息常驻栏(通知栏)的更多相关文章

  1. Android 高仿UC浏览器监控剪切板弹出悬浮窗功能

    UC浏览器应该是android手机里 最流行的浏览器之一了,他们有一个功能 相信大家都体验过,就是如果你复制了什么文字,(在其他app中 复制也有这个效果!,所以能猜到肯定是监控了剪切板),就会弹出一 ...

  2. 仿360手机卫士界面效果android版源码

    仿360手机卫士界面效果android版,这个今天一大早在源码天堂的那个网站上看到了一个那个网站最新更新的一个源码,所以就分享给大学习一下吧,布局还挺不错的,而且也很简单的,我就不把我修改的那个分享出 ...

  3. Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果

    大家好,今天给大家带来一个仿360手机卫士悬浮窗效果的教程,在开始之前请允许我说几句不相干的废话. 不知不觉我发现自己接触Android已有近三个年头了,期间各种的成长少不了各位高手的帮助,总是有很多 ...

  4. ANDROID——仿360手机卫士的旋转打分控件

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介 灵感源自360手机卫,主要功能就是实现显示评分或等级的效果.并稍微改良了一下,有更好 ...

  5. Android手机安全软件的恶意程序检测靠谱吗--LBE安全大师、腾讯手机管家、360手机卫士恶意软件检测方法研究

    转载请注明出处,谢谢. Android系统开放,各大论坛活跃,应用程序分发渠道广泛,这也就为恶意软件的传播提供了良好的环境.好在手机上安装了安全软件,是否能有效的检测出恶意软件呢?下边针对LBE安全大 ...

  6. Android 高仿 频道管理----网易、今日头条、腾讯视频 (可以拖动的GridView)附源码DEMO

    距离上次发布(android高仿系列)今日头条 --新闻阅读器 (二) 相关的内容已经半个月了,最近利用空闲时间,把今日头条客户端完善了下.完善的功能一个一个全部实现后,就放整个源码.开发的进度就是按 ...

  7. Android 高仿 频道管理----网易、今日头条、腾讯视频 (能够拖动的GridView)附源代码DEMO

    距离上次公布(android高仿系列)今日头条 --新闻阅读器 (二) 相关的内容已经半个月了.近期利用空暇时间,把今日头条client完好了下.完好的功能一个一个所有实现后.就放整个源代码.开发的进 ...

  8. android高仿抖音、点餐界面、天气项目、自定义view指示、爬取美女图片等源码

    Android精选源码 一个爬取美女图片的app Android高仿抖音 android一个可以上拉下滑的Ui效果 android用shape方式实现样式源码 一款Android上的新浪微博第三方轻量 ...

  9. (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇

    从写第一篇今日头条高仿系列开始,到现在已经过去了1个多月了,其实大体都做好了,就是迟迟没有放出来,因为我觉得,做这个东西也是有个过程的,我想把这个模仿中一步一步学习的过程,按照自己的思路写下来,在根据 ...

随机推荐

  1. Android RxJava 2.0中backpressure(背压)概念的理解

    英文原文:https://github.com/ReactiveX/RxJava/wiki/Backpressure Backpressure(背压.反压力) 在rxjava中会经常遇到一种情况就是被 ...

  2. (转)解决office软件无法卸载也无法安装的顽固问题

    原文地址 http://jingyan.baidu.com/article/f3ad7d0fcfe32509c3345bab.html 有时会出现office下载失败,然后又无法重新安装,导致offi ...

  3. 前端请求操作类型(get post put delete)

    get:获取数据 post:增加 put:修改 delete:删除

  4. 05Servlet example

    dgdfgdfggggggg Servlet 表单数据 在客户端,GET通过URL提交数据,数据在URL中可见:POST把数据放在form的数据体内提交.GET提交的数据最多只有1024字节:POST ...

  5. iOS-关于一些手势冲突问题(scrollView 嵌套 tableView)

    简单说下关于开发中容易遇到的父试图添加手势与子试图点击事件冲突,UIScrollView 嵌套 UIScrollView . UIScrollView 嵌套 UITableView的情况手势冲突问题: ...

  6. FATE HDU - 2159

    解法 完全背包但是又有别的条件(忍耐值为体力经验是价值) ①首先杀怪是有上限s的,所以需要记录杀怪的数量并且if时候还需要加上条件 ②最后还得遍历一下从小到大遍历当前dp范围内是不是已经有够经验的那么 ...

  7. TestNG套件测试(二)

    在xml中指定要运行的整个包来执行套件测试 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  8. buf.writeDoubleBE()函数详解

    buf.writeDoubleBE(value, offset[, noAssert]) buf.writeDoubleLE(value, offset[, noAssert]) value {Num ...

  9. Docker定制镜像

    定制镜像 除了使用定制好的镜像外,我们也可以通过定制实现符合自己环境的镜像. 在docker里面通过build方法来生成镜像,在生成镜像之前,我们需要一个Dockerfile脚本,脚本中包含的是一条一 ...

  10. 吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝,自己写的就当是研究咯---https://www.cnblogs.com/NieXiaoHui/p/7150928.html

    吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个ja ...