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

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF