Andriod studio 学习 之 双击退出 以及 Notitfcation 通知

Android Studio 1-3 Notitfcation 通知 与 双击退出 Android Studio 1-3一、Notitfcation 通知1、交互式的通知2、发送大图或列表通知3、发送自定义通知4、通知分组5、发送进度条通知二、双击退出当前APP 一、Notitfcation 通知 1、交互式的通知 //通知管理 NotificationManager manager1 = (NotificationManager) getSys... 阅读详情

双击退出 以及 Notitfcation 通知

双击退出

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //判断按键是返回键
        if (keyCode==KeyEvent.KEYCODE_BACK){
            //判断用户两次点击时间是否在预定时间内
            if (System.currentTimeMillis()-exittime>=2000){
                exittime=System.currentTimeMillis();
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
                //返回true时,表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理,而当返回false时表示并没有完全处理完该事件,更希望其他回调方法继续对其进行处理
                return true;
            }else {
                finish();//结束当前界面
            }
        }

        return super.onKeyDown(keyCode, event);
    }

Notification通知

oncreate里的方法都使用以下代码即可
private Button buttonid;
    //发送通知
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        buttonid = (Button) findViewById(R.id.buttonid);
        buttonid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                sendmessage();
//                usersendmessage();//自定义布局
//                progressnotify();
//                groupNotify();
//                UInotify();
                    requsetnotify();
            }
        });

    }
1.普通通知
private  void sendmessage(){
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher_background);
        builder.setContentTitle("我是标题");
        builder.setContentText("我是内容");

        //创建对象.发送的就是这个对象
        Notification build = builder.build();

        //获取通知管理器,负责发通知、清除通知等
        NotificationManager notificationManager  = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //参数一 id 通知的id(稍后介绍意义)   参数二 通知对象
        notificationManager.notify(1,build);
    }
2.自定义通知
private void usersendmessage(){
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher_background);
        builder.setContentTitle("我是标题");
        builder.setContentText("我是内容");

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout);
        remoteViews.setTextViewText(R.id.textid,"1705A");
        remoteViews.setImageViewResource(R.id.imageid,R.mipmap.ic_launcher);

        builder.setCustomContentView(remoteViews);

        Notification build = builder.build();

        notificationManager.notify(2,build);
    }
3.进度条通知
private void progressnotify(){
        final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("软件下载");

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                builder.setContentText("正在下载,当前进度:"+progress);
                builder.setProgress(100,progress,false);
                progress+=20;
                notificationManager.notify(4,builder.build());
                if (progress==100){
                    //模拟安装
                    builder.setContentText("正在安装...");
                    builder.setProgress(0,0,true);
                    //相同id才会更新
                    notificationManager.notify(4,builder.build());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    notificationManager.cancel(4);//取消置顶的通知
                    timer.cancel();
                }
            }
        },0,1000);
    }
通知分组
private void groupNotify(){
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder1 = new Notification.Builder(this);
        Notification.Builder builder2 = new Notification.Builder(this);
        Notification.Builder builder3 = new Notification.Builder(this);
        Notification.Builder builder4 = new Notification.Builder(this);

//消息分组属性,group相同才能分到一组
        builder1.setSmallIcon(R.mipmap.ic_launcher);
        builder1.setContentTitle("a");
        builder1.setContentText("A");
        builder1.setGroup("1");
        //被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息.
        builder1.setGroupSummary(true);

        builder2.setSmallIcon(R.mipmap.ic_launcher);
        builder2.setContentTitle("b");
        builder2.setContentText("B");
        builder2.setGroup("1");
//        builder2.setGroupSummary(true);

        builder3.setSmallIcon(R.mipmap.ic_launcher);
        builder3.setContentTitle("c");
        builder3.setContentText("C");
        builder3.setGroup("1");
//        builder3.setGroupSummary(true);

        builder4.setSmallIcon(R.mipmap.ic_launcher);
        builder4.setContentTitle("d");
        builder4.setContentText("D");
        builder4.setGroup("1");
//        builder4.setGroupSummary(true);

        notificationManager.notify(0,builder1.build());
        notificationManager.notify(1,builder2.build());
        notificationManager.notify(2,builder3.build());
        notificationManager.notify(3,builder4.build());
    }
锁屏通知
//builde的时候 
//通过 setVisibility() 方法设置即可
.setVisibility(VISIBILITY_PUBLIC)
.build();

setVisibility() 方法共有三个选值:
1.VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;
2.VISIBILITY_PUBLIC : 显示通知的全部内容;
3.VISIBILITY_SECRET : 不显示任何内容,包括图标。

通知的样式

大图通知 和 列表通知.以及 PendingIntent

 private void UInotify(){
        Intent intent = new Intent(this,MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");
        builder.setContentText("内容");

        builder.setContentIntent(pendingIntent);
        //震动(铃声+震动+呼吸灯)
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL);

        //列表显示
//        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//        inboxStyle.addLine("学习");
//        inboxStyle.addLine("玩耍");
//        inboxStyle.addLine("休息");
//
//        builder.setStyle(inboxStyle);

        //大图模式
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();

        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setStyle(bigPictureStyle);


        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(1,notification);

    }
PendingIntent

说明
说明
PendingIntent 是一种特殊的 Intent ,字面意思可以解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action 。从上面带 Action 的通知也能验证这一点,当用户点击通知时,才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了 PendingIntent

构造方法说明

//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);
 
//获取一个用于启动 Service 的 PendingIntent 对象
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);
 
//获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)      
PendingIntent 具有以下几种 flag:

FLAG_CANCEL_CURRENT:
如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。

FLAG_NO_CREATE:
如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。

FLAG_ONE_SHOT:
该 PendingIntent 只作用一次。

FLAG_UPDATE_CURRENT:
如果系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。

设置 Notification 的通知效果 (震动需要真机)

Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过 setDefaults(int defaults) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效。

 // 在builder的时候加上如下属性即可.
 builder.setDefaults(Notification.DEFAULT_ALL);

/设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE
 
//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND
 
//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS
 
//添加上述三种默认提醒效果
Notification.DEFAULT_ALL
更新 Notification

更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。

取消 Notification

取消通知有如下 5 种方式:

点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
如果你是通过 NotificationManager.notify(String tag, int id, Notification notify) 方法创建的通知,那么只能通过 NotificationManager.cancel(String tag, int id) 方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。

【Coppeliasim|轨迹规划(三)】机械臂逆解 文章目录一些设置导航点设置添加连接用于碰撞的设置代码实现终端输出 一些设置 导航点设置 关于如何添加设置 dummy 的细节建议参考官方手册,这里不再赘述。 添加连接用于碰撞的设置 按照序号点击(一个是机械臂,一个是所有的物体除了机械臂): 代码实现 getMatrixShiftedAlongZ=function(matrix,localZShift) local m={} for i=1,12,1 do m[i]=matrix[i] end 阅读详情

相关推荐

告别Modern Standby烦恼:在Windows 11中手动启用S3睡眠模式的完整指南

本文详细介绍了如何在Windows 11中手动启用S3睡眠模式,解决Modern Standby(S0模式)导致的笔记本睡眠后耗电问题。通过注册表修改、电源选项调整、BIOS设置等步骤,帮助用户恢复传统深度睡眠功能,显著提升电池续航表现。

weixin_30271335的博客 559

Notitfcation通知

简单的通知 代码展示 private void initView() { btn_Button = (Button) findViewById(R.id.btn_button); btn_Button.setOnClickListener(new View.OnClickListener() { @Override public void onCl...

yaojiangyu的博客 376

Cadence SIP Layout 简单教程-第一章

以摄像头模组软硬结合板为例,描述layout的整个完整过程,从而掌握基本技能。 第一章 准备工作 第一节 原理图不用说layout前需要绘制原理图,原理图不在本文讨论范围内,所以列为准备工作。 需要特别说明的是,绘制原理的最终目的是导出网络表,网络表中的重要参数footprint是让layout软件选择正确封装的关键,所以这里讲解一下原理图设置footprint的位置。在原理图中双击零件,...

whp1920的博客 1万+

Android 通知组

从 Android 7.0(API 级别 24)开始,您可以在一个组中显示相关通知。图 1.收起(顶部)和展开(底部)的通知组。注意:如果应用发出 4 条或更多条通知且未指定通知组,则系统会在 Android 7.0 及更高版本上将这些通知自动分组。分组通知必须有一个额外的通知来充当通知组摘要。要启用分组通知,必须设置通知组摘要。此通知组摘要必须包含通知组中其他通知的部分文本,以便了解通知组的内容。

Smile_729day的博客 1981

zabbix6.0.4拓扑图链路显示流量

1. 背景介绍 网络流量监控是网络管理员必备功课,以前研究cacti的weathermap插件,网络流量大小和拥塞情况一清二楚,随着cacti的没落,weathermap插件也不能再继续使用,此时就需要学习了解新的替代方案。 经过研究和实际测试,发现zabbix的拓扑图有这个功能。 以下验证了zabbix的拓扑图显示线路流量的功能。 2. Zabbix 6显示拓扑图流量过程 选择监测-拓扑图 点进Local ne...

u013930899的博客 8485

如何在 Windows 11 上安装 ChatGPT:CMD + 图形界面两种方式

我没说在 Windows 11 上装 ChatGPT 应用有多难,但你要是知道有哪几种官方途径可用,事情就会顺一点,尤其是对新手。对大多数用户来说,商店应用就是正确的选择。如果你在受管理的设备上,或者想避开商店,那么通过 Edge 或 Chrome 装 PWA 是完全站得住脚的替代方案。不管你选哪条路,真正的收获都是把 ChatGPT 从浏览器标签页里挪出来,放进一个它真正像个工具的地方。把它固定到任务栏、配好 Alt + 空格,你就会纳闷以前没它是怎么干活的。

山岚的运维笔记 299

双击退出,Notification 通知

双击退出,Notitfcation 通知一、双击退出二、Notitfcation 通知setSmallIcon() 与 setLargeIcon()发送一个简单的通知发送一个自定义的通知发送一个进度条通知通知分组大图通知 一、双击退出 实现的基本原理就是,当按下BACK键时,会被onKeyDown捕获,判断是BACK键,则执行exit方法。 判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。 import androidx.appcompat.app.Ap

SeeYouTomorrow_s的博客 393

LAMMPS+Phonopy计算声子色散曲线

机器学习训练完势函数之后,需要对加以验证。对于晶体而言,色散曲线是其衡量计算是否准确的最重要标准,当然,还有能量,比热,RDF等性质。

qq_68842798的博客 3462

每天记录学习的新知识 :通知(Notification)的使用方法

问题: 点击顶部通知栏折叠部分会重启app: 转载: http://www.luyixian.cn/news_show_234093.aspx notification = new Notification.Builder(AppStart.getContext(), CHANNEL_ID) .setChannelId(CHANNEL_ID) .setContentTitle(title) ...

嗯... 2532

在Windows11上安装ubuntu版linux系统并实现桌面图形化(WSL)

在Windows11上安装ubuntu版linux系统并实现桌面图形化(WSL),xfce桌面美化、安装google-chrome

NiKoJJ‘s Blog 1万+

别再让OCV拖慢你的芯片!手把手教你用set_timing_derate优化STA时序(附CPPR实战)

本文深入探讨了芯片设计中OCV(On-Chip Variation)对时序分析的影响,并提供了使用`set_timing_derate`优化STA时序的实战指南。通过分阶段derate设置和CPPR技术,有效减少时序违例,提升芯片性能。文章结合TSMC 7nm工艺案例,详细解析了OCV建模与优化策略,助力工程师实现时序收敛。

weixin_42525005的博客 305

Android N 通知栏和快捷通知栏带来的改变

Android N 通知栏和快捷通知栏带来的改变Android N 引入了一些新的API,允许应用发布具有高度可见性和交互性的通知。 Android N 扩展了现有 RemoteInput 通知 API,以支持手持式设备上的内联回复。 此功能允许用户从通知栏快速进行回复,无需访问应用。此外,Android N 还允许捆绑类似的通知并将它们显示为一则通知。 为了实现此功能,Android N 使用现

code change the world 6272

对角两对双差速轮AGV的平移、直行、转弯和原地旋转之PID控制

本示例将展示如何使用PID控制算法,控制具有对角线布置的两对双差速轮(左前和右后)的AGV,实现平移、直行、转弯和原地旋转等运动。通过对车辆的运动学和PID控制器的设计,我们可以精确地控制AGV的运动

xiaoc100200的博客 1511

Android 7.0消息通知超过3条合并引发的问题

问题描述: 在Android 7.0以上的版本, 同一个App的消息通知超过3条时会合并到一起, 经过我对部分机型的测试, 目前只发现小米手机在点击合并到一起的消息时会将消息列表展开, 其他品牌都是启动App, 不能跳转到我们需要的指定页面并且消息不会自动消失。 解决方案: 针对Android 7.0以上的设备可以采用如下方法强制不合并消息, 亲测有效 NotificationCompat....

qq1073273116的博客 4197

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

android中的notificatio

先获得NotificationManager,然后通过PendingIntent 发出nofitication,在putextr里面把想通知的事情在里面说清楚,有4中notification 1、sendBasicNotification 2、sendBigTextStyleNotification 3、sendBigPictureStyleNotification 4、sendInbox

TonyJiang08的专栏 1071

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

android之notifiation的例子

通知用于在顶部显示类似ios的push通知之类的,下面是sample代码 1 package com.example.test; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import a...

weixin_33885676的博客 230

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

android捆绑demo_Android捆绑通知

android捆绑demoIn this tutorial, we’ll be implementing Bundled Notifications in our Android Application. We’ve seen and implemented Direct Reply for Android Nougat. If you aren’t aware about Notificatio...

从零开始的教程世界 778

Android Studio实现activity传值,Handle,Notification三个功能

第三次作业: 1 实现两个activity之间的传值 2 利用Handle机制实现多线程 3 实现普通通知,带进度条的通知,带按钮的通知 1 实现两个activity之间的传值: 顾名思义,要建立两个activity,在运行第一个activity时能打开第二个activity,并且把第二个activity的某项值返回给第一个activity。 这里以“去哪旅行”为例,建立第一个activity——...

Wendy__Xu的博客 1204

双击退出,Notitfcation 通知

双击退出,Notitfcation 通知 1.双击退出 /** * 该方法用来捕捉手机键盘被按下的事件。 * @param keyCode 该参数指的是被按下的键的键盘码,手机键盘中每个按钮都会有其对应的键盘码, * 在应用程序都是通过键盘码才知道用户按下的是哪个键。 * @param event 当用户按下按键时,...

weixin_45375472的博客 199

dev GridView 的组计和分组计

/// <summary> /// //添加组计 /// </summary> private void SetGroupSummary(GridView gv, string fieldName, GridColumn showInColumn, string displayFormat) ...

dhmwidg13313的博客 670

android-Notification.Builder

Notification.Builder public static class Notification.Builder  extends Object  java.lang.Object    ↳ android.app.Notification.Builder Builder class for Notification obj

燃烧那滴泪的专栏 5302

Notification通知

定义及用法 定义: 是在系统的通知栏中呈现多样式持久性消息的类 用处: 显示客户端的推送消息(如有新版本发布、广告、推荐新闻等) 显示正在进行的事物(如音乐播放器、版本更新时候的下载进度等) 显示接收到短消息,即时消息等信息(如QQ、微信、新浪、短信) 常用方法 setContentTitle(); // 设置标题 setContentText(); // 设置内容 setWhen(); //...

氵水 氵 726

Android之Android N 上的notification归类功能

Android之Android N 上的notification归类功能文章链接:http://blog.youkuaiyun.com/qq_16628781/article/details/65938948知识点: Android通知的优缺点及改善方法; Android N中使用通知归类功能; 新名词记录{StatusBarNotification:每一个通知的状态类,包含notification原始的tag

姚镜堂 8783

android 4.4 通知显示,Android:分组通知和摘要仍然分别显示在4.4和以下

Android:分组通知和摘要仍然分别显示在4.4和以下我想在Android Wear上实施堆叠通知为此,我为每个“商品”创build了1个摘要通知和N个单独通知。 我只想要摘要在手机上显示。 这是我的代码:private void showNotifications() { NotificationManager notificationManager = (NotificationManage...

weixin_32390279的博客 587

Andriod studio学习 之 对话框

学习第一天Dialog对话框一 常用的对话框1.普通对话框2.单选对话框3.多选对话框4.自定义对话框5.水平进度对话框6.圆形进度条对话框7.日期选择对话框8.普通对话框二 自定义对话框1.思路:2.代码 Dialog对话框 一 常用的对话框 1.普通对话框 //普通对话框 public void normal_dialog(){ WindowManager.LayoutParams lp =...

Young_cloud的博客 526

Android 8.0 Notifictio 通知音

final String CHANNEL_ID = "com.chao.channel.id"; final String CHANNEL_NAME = "com.chao.channel.name"; NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Noti...

王人冉的博客 404
上一篇: Andriod studio 学习 之 Menu菜单与PopupWindow弹出窗体
下一篇: Andriod studio 学习 之 Fragment基础知识
Young_cloud
博客等级 码龄7年 18粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值