Android的自定义notification选项我觉得限制还是挺多的,如图所示,官方API告诉我们它依然必须得设置icon,tittle,text三个选项,除此之外,还一定要设置pengdingintent,不少网友还反映 builder.setContent(remoteViews)后面一定要紧跟着builder.setContentIntent(pendingIntent),要不然会报错误:android.app.RemoteServiceException: Bad notification posted from package。

MainActivity代码

package com.example.f04_notification;import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;public class MainActivity extends Activity {private NotificationManager manager;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)this.findViewById(R.id.button1);//获取系统服务manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubNotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.notifiaction);//因为android api的限制,自定义通知仍需要set small icon,setContentTittle,setContentTextbuilder.setSmallIcon(R.drawable.ic_launcher);builder.setContentText("hello world!");builder.setContentTitle("Android");//以上内容仍必须设置一遍remoteViews.setTextViewText(R.id.textView1, "通知来了");remoteViews.setImageViewResource(R.id.imageView1, R.drawable.ic_launcher);Intent intent=new Intent(MainActivity.this,MainActivity.class);//pendingintent的flag分为四个FLAG_CANCEL_CURRENT,FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT   PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);builder.setContent(remoteViews);builder.setContentIntent(pendingIntent);manager.notify((int)System.currentTimeMillis(), builder.build());}});}}

布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/imageView1"android:contentDescription="@string/hello_world"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="27dp"android:layout_marginTop="33dp"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/imageView1"android:layout_marginLeft="76dp"android:layout_toRightOf="@+id/imageView1"android:text="@string/hello_world" /></RelativeLayout>

感谢大家的阅读,希望跟大家一起互相学习,互相进步!

Android开发之自定义Notification(源代码分享)相关推荐

  1. android自定义横竖双向滚动,Android开发实现自定义水平滚动的容器示例

    Android开发实现自定义水平滚动的容器示例 发布时间:2020-09-12 01:25:56 来源:脚本之家 阅读:71 作者:CharlinGod 本文实例讲述了Android开发实现自定义水平 ...

  2. android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例

    本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...

  3. 《Android开发卷——自定义日期选择器(三)》

                 继 <Android开发卷--自定义日期选择器(一)>:http://blog.csdn.net/chillax_li/article/details/19047 ...

  4. 《Android开发卷——自定义日期选择器(二)》

    (小米手机) (中兴手机) 在上一篇中,我介绍了一般公司都会自定义时间日期选择器,并结合自己所做的项目给大家参考. 工作实录之<Android开发卷--自定义日期选择器(一)>链接:htt ...

  5. 【Android开发】自定义圆角button样式

    [Android开发]自定义圆角button样式 结果图 步骤 在res/drawable中新建一个drawable文件,自定义命名为btu.xml; 在btu.xml中设置圆角大小和填充颜色,代码如 ...

  6. 【Android开发】自定义ListView,使用通用适配器,并实现ListView上的每一项和每一项上的按钮等控件同时监听

    ListView在Android开发中是比较常用的系统组件,但是有时候我们除了需要做ListView上每一行的点击监听事件之外,如果每一行上还有其他需要监听的控件例如Button.CheckBox等, ...

  7. Android开发丶集成微信原生分享并于分享网页时加载网络缩略图

    微信分享也是一项很常用的功能了,以往都是用友盟或者mobShareSDK框架来实现的,这两者在微信官方的sdk基础上封装的很好,并且加入了一些很实用性的功能,不过这次因为只有微信平台的分享,而且微信登 ...

  8. Android开发之通知栏Notification详解

    Notification的用法  --- 状态栏通知  发送一个状态栏通知必须的两个类:            1. NotificationManager   --- 状态栏通知的管理类,负责发通知 ...

  9. Android开发,自定义View的学习合集

    转载自:http://blog.csdn.net/u011507982/article/details/51199644 自定义控件学习  https://github.com/GcsSloop/An ...

最新文章

  1. 自定义PHP错误报告处理方式
  2. tf.keras.layers.Embedding 嵌入层 示例
  3. AI芯片大战已然打响,国内外巨头抢占万亿智能家居市场
  4. android自定义控件绘制位置,Android自定义控件之——文字圆形边框(将文字绘制在圆中间)...
  5. 9个妙招增强家庭WIFI信号
  6. c++ 11 override final
  7. 迁移数据库到SQL on Linux Docker
  8. 网页静态服务器-1-显示固定的页面
  9. oracle复杂分组查询语句,oracle中的“复杂”分组统计sql
  10. php算法求出一个数可以被分解成多少个_小学数学必考的34个数学重难点公式,赶紧给孩子收藏!...
  11. bootstraptable 一条数据跨行_据说它是唐山市拥有奶茶店最多的一条街,你一定也逛过!...
  12. java 农历公历转换_JAVA版农历和阳历相互转换源码
  13. matlab生成Z为常数的.grd格式数据
  14. windows 系统arp命令
  15. 用python学概率与统计(第十二章)拟合度检验和独立性检验
  16. 小红书七夕营销攻略,玩出新花样(内附小红书推广方案干货)
  17. 电脑快捷键你知道多少个?QQ截图快捷键ctrl加什么
  18. 未来科技 从零到一「真格星球 · 前沿科技创业营」探访加速科技
  19. Resharper快捷键
  20. 【C/C++牛客每日必刷】--- 牛客刷题系列

热门文章

  1. 开机后需要手动打开mysql_mysql解压版一键配置
  2. python完美立方数_Python练习实例3 | 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?...
  3. 画一个空心圆_用SolidWorks画一个空心挂钩,这种画法稍微有点麻烦
  4. 军队可以用oracle,使用Oracle JRockit 提高tomcat性能
  5. 如何将response里header的date转化为当地时间_外贸独立站卖家如何选择收款方式?...
  6. linux启动自动挂载共享文件,linux中自动挂载windows 共享目录
  7. php 记事本源代码_php实现记事本案例
  8. mfc获取鼠标在其他窗口中坐标_C井编程,稍加修改,将之前“会跑的按钮”改成“会跑的窗口”...
  9. (轉貼) 美電腦工程師改寫遊戲軟體向女友求婚成功 (News)
  10. BeautfuiSoup4解析器