业界动态
android24 是什么手机,【Android】24.0 手机多媒体(二)——android是什么手机「android24 是什么手机,【Android】24.0 手机多媒体(二)——」
2025-01-11 22:17

关于通知(Notification)相关的知识点,会通过层层递进的原则,拆封成4-5篇文章,大抵分为:发出状态栏通知

响应状态栏通知点击事件

通知进阶篇

通知高级篇

这样可以让理解整个知识,变得很容易,迅速掌握。

3.0 响应状态栏通知点击事件,只需要一个知识点:PendingIntent

PendingIntent估计是Intent的“表兄弟”。不同点在于:Intent倾向于立即执行某个动作

PendingIntent更加倾向于在某个合适的时机去执行某个动作

可以把PendingIntent简单理解为:延迟执行的Intent

4.0 PendingIntent的用法:用法也很简单。

4.1 它主要通过几个静态方法获得 PendingIntent实例getActivity( ) 响应活动

getBroadcast( ) 响应广播

getService( ) 响应服务

等等(可能有其他,没去查文档)

4.2 这几个方法需要接收的参数是相同的

比如:PendingIntent.getActivity(Contex,RequestCode,Intent,Flags) 接收4个参数:第1个参数是Context,没什么多说的

第2个参数一般用不到,传0即可

第3个参数是一个Intent对象,可以通过这个Intent对象构建出PendingIntent的意图。

本篇项目中的Intent表达了我要启动NotificationManager活动的企图,很简单。

第4个参数是用于确定PendingIntent的行为,有4个默认值,通常情况下传入0即可。

4.3 还是科普一下这4个参数吧(可以跳过去不看,不是重点):- 1.0 FLAG_CANCEL_CURRENT:    如果当前系统中已经存在一个相同的 PendingIntent 对象

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

系统将不会创建该 PendingIntent 对象而是直接返回 null 。- 3.0 FLAG_ONE_SHOT:    该 PendingIntent 只作用一次。- 4.0 FLAG_UPDATE_CURRENT:    如果系统中已存在该 PendingIntent 对象

那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前

PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。

5.0 在Notificationtest项目中,右击com.example.notificationtest包NewActivityEmpty Activity,新建NotificationTest,布局名称起名为activity_notification_test.xml(事实上,我用的完全是系统自己的默认命名)

修改activity_notification_test.xml中的代码,里面就只有一个很简答的文本显示控件,如下所示:<?xml  version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".NotificationTest">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="这是一个通知布局"

android:textSize="24sp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

NotificationTest.java就搞定了。

6.0 修改MainActivity中的代码:package com.example.notificationtest;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.graphics.BitmapFactory;import android.os.Build;import android.support.annotation.RequiresApi;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private String id "channel_001";    private String name "name";    @Override

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button sendNotice = (Button) findViewById(R.id.send_notice);

sendNotice.setonClickListener(this);

}    @RequiresApi(api = 26)    @Override

public void onClick(View v) {        switch (v.getId()) {            case R.id.send_notice:

Intent intent = new Intent(this, NotificationTest.class);                //PendingIntent.getActivity 接收4个参数

// 第1个参数是Context

// 第2个参数一般用不到,传0即可

// 第3个参数是一个Intent对象,可以通过这个对象构建出PendingIntent的意图

// 这里的Intent表达了我要启动NotificationManager活动的企图

// 第4个参数是用于确定PendingIntent的行为,有4个默认值,通常情况下传入0即可

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);                //第一步:获取状态通知栏管理

NotificationManager manager =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = null;                //第二步:实例化通知栏构造器NotificationCompat.Builder

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API

NotificationChannel mChannel = new NotificationChannel(id, name,

NotificationManager.importANCE_LOW);

manager.createNotificationChannel(mChannel);

}

notification = new NotificationCompat.Builder(this, id)

.setContentTitle("这是一个内容标题")//设置通知栏标题

.setContentText("这是一个内容文本") //设置通知栏显示内容

.setWhen(System.currentTimeMillis())//通知产生的时间。

// 会在通知信息里显示,通常是系统获取到的时间

.setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON

.setLargeIcon(BitmapFactory.decodeResource(getResources()

, R.mipmap.ic_launcher))//设置通知大ICON

.setContentIntent(pi)

.setAutoCancel(true)

.build();                //第三步:对Builder进行配置

manager.notify(1, notification);                break;            default:                break;

}

}

}

本质上就增加了4行代码:代码块备注://第一步:获取状态通知栏管理:上面有2句,还有一堆注释,不再赘述。

notification = new NotificationCompat.Builder(this, id)... 里面最后的.build();前面家了1句.setContentIntent(pi)和1句.setAutoCancel(true).setContentIntent(pi) 这句话的意思是:我,“伟大的PendingIntent大人”需要接收一个PendingIntent对象,当用户点击状态栏那条通知的时候,我就会执行里面相应的逻辑(打开NotificationTest活动界面)

.setAutoCancel(true) :这个涉及到一个独立的知识点,在8.0里面单独讲解

7.0 我们先把.setAutoCancel(true) 注释掉,执行项目,结果如下

AAffA0nNPuCLAAAAAElFTkSuQmCC

2019-02-21_221238.png

AAffA0nNPuCLAAAAAElFTkSuQmCC

2019-02-21_221244.png

点击状态栏的通知条

AAffA0nNPuCLAAAAAElFTkSuQmCC

2019-02-21_221252.png

8.0 这里有一个细节,大家可能没注意到

在上面第三张图片中,状态栏通知点击事件明明已经成功响应了,但是通知并没有消失(注意右上角,那个“小白点”一直在,下拉查看一下,的确该通知执行完之后并没有“听话地"消失掉)

解决方案有两个:一是上面的第6.0最后的.setAutoCancel(true),加了这个,点击通知后,进入新的界面,通知栏那条通知消失。

二是显式调用NotificationManager的cancel( )方法。实现的方式是在NotificationTest.java中增加两行代码:package com.example.notificationtest;import android.app.NotificationManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class NotificationTest extends AppCompatActivity {    @Override

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);

setContentView(R.layout.activity_notification_test);        //就是下面这2行

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

manager.cancel(1);

}

}

解释一下,我们在cancel( )方法中传入1,这个1的意思是:还记得在创建通知是给每条通知指定的id么

AAffA0nNPuCLAAAAAElFTkSuQmCC

当时给这条通知设定的id就是1,所以如果你想取消哪条通知,就在cancel( )方法中传入那个通知的id就行了。

作者:我睡醒刚刚

    以上就是本篇文章【android24 是什么手机,【Android】24.0 手机多媒体(二)——android是什么手机「android24 是什么手机,【Android】24.0 手机多媒体(二)——」】的全部内容了,欢迎阅览 ! 文章地址:http://fabua.ksxb.net/news/6629.html 
     文章      相关文章      动态      同类文章      热门文章      栏目首页      网站地图      返回首页 海之东岸资讯移动站 http://fabua.ksxb.net/mobile/ , 查看更多   
最新文章
明确0到2级辅助驾驶系统 “人机共驾”法律属性
公安部交通管理局局长王强7月23日在国新办举行的“高质量完成‘十四五’规划”系列主题新闻发布会上表示,为进一步加强规范管理
iPhone 17系列价格曝光:仅一款不涨价 Pro版更具性价比
【TechWeb】近段时间以来,全新iPhone 17系列尤为吸引外界的目光,而根据供应链最新爆料,iPhone 17系列距离亮相仅剩差不多一个
管庄回收茅台酒!管庄茅台酒回收!
137=1888=0048 回收茅台礼盒15年 30年 50年 80年茅台礼盒回收洋酒系列 回收路易十三,轩尼诗,拉菲,拉图,马爹利,人头马
北京到陶乐物流专线价格,货到付款
长胜物流24 小时服/务热线:||长途搬家|家电托运|电脑托运|液晶电视托运|冰箱托运|洗衣机托运|行李托运|电动车托运|摩托车托运|
解读Solana最新技术路线图:锚定“互联网资本市场”,打造链上华尔街
昨天 Solana 宣布了新的路线图。本质上,由于各链改进都已进入深水区,导致确实有些名词堆砌的情况,我试着用大家可以理解的方式
国泰海通发布新一代全AI智能APP灵犀
央广网北京7月26日消息(记者樊瑞)当前,人工智能技术正加速重构证券行业服务业态,加快发展新一代人工智能是推动证券行业高质
新加坡地道特色平价美食,好吃不踩雷
新加坡太多美食了~亚坤-咖椰吐司$5左右,这是我在新加坡吃的第一顿早餐。不知道好不好吃,但看着一大早就好多人排队,那就没错
上海UPS蓄电池回收
上海UPS回收,UPS电源回收,UPS电池回收:上海APC不间断电源回收,UPS主机回收,机房设备回收,回收UPS设备,UPS蓄电池回收,UPS
云边端协同:虚拟电厂的“神经网”革命
上海某高校586台空调在50秒内无感降负300kW,精度96.57%;重庆虚拟电厂调度17MW可调资源,延迟压缩至100ms级——这组数据的背后
高金食品与远方好物达成战略合作,让高品质“真黑猪”走进千家万户的餐桌
中国黑猪看四川,四川黑猪看高金。高金食品正用“真黑猪”,破局本土黑猪市场占有率困境,开创中国黑猪产业新篇章。7月23日,在