notification的几种使用方法

2024-06-04 21:48

本文主要是介绍notification的几种使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Notification的使用

代码下载链接:http://download.csdn.net/detail/u011324501/9295057

方法1

public void notifi(){
NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);// Intent intent = new Intent(this, MainActivity.class);
//  PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
//新建一个notification实例Notification.Builder builder = new Notification.Builder(getApplicationContext());// builder.setContentIntent(pIntent);builder.setSmallIcon(R.drawable.fsad);//设置小标题
//获取自定义布局RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.notification);
//获取布局idremoteView.setImageViewResource(R.id.image, R.drawable.fsad);remoteView.setTextViewText(R.id.title, "USB storage connected");remoteView.setTextViewText(R.id.content, "Click to FileManager");remoteView.setTextViewText(R.id.btn,"Remove");//Intent active = new Intent(this, MyReceiver.class);Intent active = new Intent("com.aaa");//发送广播// MainActivity.this.sendBroadcast(intent);// active.setAction(ACTION_NOTIFY_BUTTON);PendingIntent pintent2 = PendingIntent.getBroadcast(this, 0, active, 0);
//给广播添加监听器remoteView.setOnClickPendingIntent(R.id.btn, pintent2);Notification notification = builder.build();notification.contentView = remoteView;nm.notify(1000, notification);// NotificationManager manger = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
//  manger.cancel(1000);//取消notification
}

方法2

public void notification(){
int icon = R.drawable.ic_launcher;  CharSequence tickerText = "Notification01";  long when = System.currentTimeMillis();  Notification notification = new Notification(icon,tickerText,when);  RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setImageViewResource(R.id.image, R.drawable.fsad);contentView.setTextViewText(R.id.title, "USB storage connected");  contentView.setTextViewText(R.id.content, "Click to FileManager");  // contentView.setTextViewText(R.id.btn,"btn");contentView.setCharSequence(R.id.btn, "setText", "remove");Intent active = new Intent("com.aaa");// notification.contentView = contentView;  notification.bigContentView =contentView;  PendingIntent pintent2 = PendingIntent.getBroadcast(getApplication(), 0, active,0);contentView.setOnClickPendingIntent(R.id.btn,pintent2);String ns = Context.NOTIFICATION_SERVICE;  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);  mNotificationManager.notify(1000, notification); 
}

方法3

可以自定义notification的高度
public void no(){
//大图标
Bitmap btm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
//Intent intent = new Intent(MainActivity.this, ResultActivity.class);
Intent active = new Intent("com.aaa");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, active,0);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 
contentView.setCharSequence(R.id.btn,"setText", "Remove");
Notification noti = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)//小标题
.setLargeIcon(btm)//大标题
.setNumber(13)
.setContentIntent(pendingIntent)
.setStyle(
new NotificationCompat.InboxStyle()
.addLine("Click to FileManager") .setBigContentTitle("USB storage connected") .setSummaryText("Remove"))
.build();RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.notification);remoteView.setImageViewResource(R.id.image, R.drawable.fsad);remoteView.setTextViewText(R.id.title, "USB storage connected");remoteView.setTextViewText(R.id.content, "Click to FileManager");remoteView.setTextViewText(R.id.btn,"Remove");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1000, noti);
}

方法4

可自定义notification自定义高度

public void nn(){Notification foregroundNote;
RemoteViews contentView = new RemoteViews(this.getPackageName(),R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.fsad);contentView.setTextViewText(R.id.title, "USB storage connected");  contentView.setTextViewText(R.id.content, "Click to FileManager");  contentView.setTextViewText(R.id.btn, "REMOVE");Notification.Builder mNotifyBuilder = new Notification.Builder(this);
foregroundNote = mNotifyBuilder.setContentTitle("USB storage connected")//一级标题.setContentText("Click to FileManager")//二级标题.setSmallIcon(R.drawable.fsad)// .setLargeIcon(btm)//大图标.build();
Intent active = new Intent("com.aaa");
foregroundNote.bigContentView = contentView;
PendingIntent pintent = PendingIntent.getBroadcast(getApplication(), 0, active,0);contentView.setOnClickPendingIntent(R.id.btn,pintent);
NotificationManager m = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
m.notify(1000, foregroundNote);
}

移除notification方法:

public void removenm(){
NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);nm.cancel(1000);
}

接收发送出来的广播:

public static class MyReceiver extends BroadcastReceiver {@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 实例化Intent  
if(intent.getAction().equals("com.aaa")){
data();
Toast.makeText(context, "receiver", Toast.LENGTH_SHORT).show();
}
}}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>

Main.xml布局代码:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"/>
<Buttonandroid:id="@+id/remove"android:layout_below="@+id/btn"android:layout_width="fill_parent"android:layout_height="wrap_content"/>
</RelativeLayout>

自定义notification布局:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:background="#ffffff"android:layout_height="wrap_content">  <ImageViewandroid:id="@+id/image"android:layout_marginTop="10dip"android:layout_width="50dip"android:layout_marginLeft="8dip"android:layout_alignParentLeft="true"android:layout_height="40dip"/><LinearLayout android:id="@+id/linearlayout"android:layout_marginTop="7dip"android:layout_marginLeft="5dip"android:layout_toRightOf="@+id/image"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView android:id="@+id/title"android:textColor="#000000"android:textSize="16sp"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView android:id="@+id/content"android:textColor="#999999"android:textSize="14sp"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout><TextViewandroid:id="@+id/view"   android:layout_below="@+id/linearlayout"android:layout_marginTop="10dip"android:layout_marginLeft="60dip"android:layout_width="fill_parent"android:layout_height="1dip"android:background="#999999" /><TextView   android:id="@+id/btn"  android:layout_marginTop="6dip"android:background="#ffffff"android:textSize="18sp"android:layout_marginLeft="60dip"android:layout_width="wrap_content"  android:layout_height="40dip"  android:layout_below="@+id/view"android:textColor="#999999"android:layout_marginRight="10dp"  />  </RelativeLayout>  

广播的注册:

//接收广播在MainActivity里面

<receiver android:name="com.example.boradcastreceiver.MainActivity$MyReceiver"> <intent-filter> <action android:name="com.aaa"/> </intent-filter> </receiver> //直接是广播一个文件<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="com.noti"/> </intent-filter> </receiver> 


这篇关于notification的几种使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1031216

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依