Android Notification.Builder通知案例分享

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

随着Android系统的不断升级,相关Notification的用法有很多种,有的方法可能已经被android抛弃了,下面为大家分享一下个人如何实现Android Notification通知小案例源代码,供大家参考。

Android Notification.Builder通知小案例,具体代码示例如下:

package com.example.day6ke;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
  private Button btn;
  private NotificationManager Manager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn= (Button) findViewById(R.id.bnt);

    Manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    btn.setOnClickListener(new View.OnClickListener() {
      @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
      @Override
      public void onClick(View v) {
        Notification.Builder builder=new Notification.Builder(MainActivity.this);
        builder.setContentTitle("通知");
        builder.setContentText("正文");
        builder.setSmallIcon(R.mipmap.ic_launcher);


        //的到PendingIntent(延迟意图)的对象
        PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 2020,new Intent(MainActivity.this, Main2Activity.class), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);
        //点击通知是否消失   该方法与PendingIntent 一起使用才有效果
        builder.setAutoCancel(true);
        Manager.notify(1,builder.build());
      }
    });
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多