android 通知Notification详解及实例代码

所属分类: 软件编程 / Android 阅读数: 23
收藏 0 赞 0 分享

android Notification实例详解

1.使用Builder模式来创建

2.必须要设置一个smallIcon,还可以设置setTicker

3.可以设置 setContentTitle,setContentInfo,setContentText,setWhen

4.可以设置setDefaults(闪屏,声音,震动),通过Notification设置flags(能不能被清除)

5.发送需要获取一个NotificationManager(getSystemService来获取);notify(int id,Notification)

6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能访问,需要导入v4包,使用notificationCompat兼容类。

自定义通知

使用RemoteViews来建立一个布局,然后使用setContent()设置;

点击事件使用PendingIntent来完成

下面是一个案例

MainActivity类

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  public void clearNotification(View v) {
    // 通过代码来清除 NO_CLEAR
    // 清除也需要manager
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 只能清除由本应用程序发出去的通知
    manager.cancelAll();
  }

  public void sendNotification(View v) {
    // 他是 在v4包中的通知,用于16以下版本发送通知
    // NotificationCompat
    // 通知的创建
    Notification.Builder builder = new Notification.Builder(this);
    // NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
    // this);
    // 显示在通知条上的图标 必须的
    builder.setSmallIcon(R.drawable.ic_launcher);
    // 显示通知条上的文字 不是必须的
    builder.setTicker("您有一条新的消息!!");

    // 状态栏中的
    builder.setContentTitle("大标题");
    builder.setContentText("文本");
    builder.setWhen(System.currentTimeMillis());
    builder.setContentInfo("Info");
    builder.setDefaults(Notification.DEFAULT_ALL);
    // 点击事件使用Pending
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pi);
    // 生成对象 16API以上,支持低版本需要使用v4包中的notificationCompat
    Notification notify = builder.build();
    // 设置不能清除
    notify.flags = Notification.FLAG_NO_CLEAR;

    // 如何将通知发送出去
    NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 通知的唯一值,如果id重复,表明是更新一条通知,而不是新建
    mananger.notify((int) (Math.random() * 1000), notify);
  }

  public void diyNotification(View v) {
    // 展示在通知上面的视图
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
    Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher).setTicker("自定义通知 ")
        // 布局
        .setContent(views).build();
    // 使用RemoteViews来设置点击事件
    views.setTextColor(R.id.tv, Color.RED);
    Intent intent = new Intent(this, OneActivity.class);
    // 音乐播放是放在Service
    PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.tv, pi);

    Intent intent2 = new Intent(this, MainActivity.class);

    PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.iv, pi2);
    // 发送
    NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notify.notify(1, notification);

  }

}

OneActivity类

public class OneActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("跳转界面");
    setContentView(tv);
  }
}

activity.main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.lesson8_notification.MainActivity" >

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendNotification"
    android:text="普通的通知" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clearNotification"
    android:text="清除所有通知" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="diyNotification"
    android:text="自定义通知" />

</LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

最后记得注册activity

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多