android 通知Notification详解及实例代码

所属分类: 软件编程 / Android 阅读数: 46
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多