安卓q bubbles

In this tutorial, we’ll look at a new feature introduced with Android Q which is Bubbles. We’ll be implementing it in our Android Application today.

在本教程中,我们将介绍Android Q引入的一项新功能,即Bubbles。 今天,我们将在我们的Android应用程序中实现它。

Android Q Bubbles (Android Q Bubbles)

We’ve all seen how Facebook chat bubbles work. Come Android Q, and now we have a built-in notification system which opens a preview of the application screen from the notification bubble. Moreover, we can multi-task and switch between different bubbles.

我们都已经看到了Facebook聊天气泡是如何工作的。 快来看看Android Q,现在我们有了一个内置的通知系统,它可以从通知气泡中打开应用程序屏幕的预览。 此外,我们可以执行多任务并在不同气泡之间切换。

When the device is locked, Bubbles won’t be displayed. Only the normal notification would be displayed.
Bubbles are an opt-in feature. When presented for the first time, we have an option to allow/disable bubbles.
Otherwise, we can do the same from the settings.

设备锁定后,将不会显示气泡。 仅显示普通通知。
气泡是一项可选功能。 首次显示时,我们可以选择允许/禁用气泡。
否则,我们可以从设置中执行相同的操作。

Bubble displays content in floating windows.

气泡在浮动窗口中显示内容。

Android Q Notification Bubble Permissions

Android Q通知气泡权限

气泡如何实施? (How are Bubbles implemented?)

In order to implement Bubbles in our Notification, we have to set the Bubble Metadata using the Builder and set it on the Notification with setBubbleMetadata().

为了在我们的Notification中实现Bubbles,我们必须使用Builder来设置Bubble元数据,并使用setBubbleMetadata()在Notification上setBubbleMetadata()设置。

For the activity to open from the bubble, we need to define it in the Manifest file as:

为了使活动从气泡中打开,我们需要在清单文件中将其定义为:

<activityandroid:name=".BubbleActivity"android:label="@string/title_activity_bubble"android:allowEmbedded="true"android:documentLaunchMode="always"android:resizeableActivity="true"android:theme="@style/AppTheme.NoActionBar"/>

Creating a Bubble Metadata:

创建气泡元数据:

Notification.BubbleMetadata bubbleData =new Notification.BubbleMetadata.Builder().setDesiredHeight(600).setIntent(bubbleIntent).setAutoExpandBubble(true).setSuppressInitialNotification(true).build();

But you can always auto expand the bubble by setting the methods
但是您始终可以通过设置方法setAutoExpand(true) and setAutoExpand(true)setSuppressInitialNotification(true)setSuppressInitialNotification(true)自动扩大气泡

The lifecycle of the activity presented in the Bubble is the same as the normal lifecycle.
Every time the bubble is dismissed, the activity gets killed.

气泡中显示的活动的生命周期与正常生命周期相同。
每次消除气泡,该活动就会被杀死。

For a bubble to be displayed, you must pass IMPORTANCE HIGH in the NotificationChannel.

要显示气泡,必须在NotificationChannel传递IMPORTANCE HIGH

canBubble() method. You can use this to determine whether bubbles could be displayed for this notification channel or whether they are disabled.canBubble()方法。 您可以使用它来确定是否可以为该通知通道显示气泡或是否将其禁用。

setAllowBubbles() can be set to true on the NotificationChannel in order to allow bubbles to be displayed for this NotificationChannel group.

可以在NotificationChannel setAllowBubbles()设置为true,以允许为此NotificationChannel组显示气泡。

PRO-TIP
areBubblesAllowed() is added in the NotificationManager class. This along with canBubble should be used to check whether bubbles are allowed or not.

专家提示
在NotificationManager类中添加了areBubblesAllowed() 。 可以将它与canBubble一起使用,以检查是否允许气泡。

In the following section, we’ll be creating a simple application that demonstrates Android Bubbles.
We’ll be using AndroidX.

在以下部分中,我们将创建一个演示Android Bubbles的简单应用程序。
我们将使用AndroidX。

项目结构 (Project Structure)

Android Q Bubble Project Structure

Android Q Bubble Project Structure

Android Q Bubble项目结构

(Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:gravity="center"android:layout_height="match_parent"><Buttonandroid:id="@+id/btnBubble"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Create Bubble" /><Buttonandroid:id="@+id/btnBubble2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Another Bubble" /></LinearLayout>

The code for the MainActivity.java class is given below:

MainActivity.java类的代码如下:

package com.journaldev.androidqbubbles;import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Button btnBubble, btnBubble2;NotificationManager notificationManager;Notification.Builder builder;NotificationChannel channel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnBubble = findViewById(R.id.btnBubble);btnBubble2 = findViewById(R.id.btnBubble2);notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);CharSequence name = "My Channel";String description = "xyz";int importance = NotificationManager.IMPORTANCE_HIGH;channel = new NotificationChannel("1", name, importance);channel.setDescription(description);channel.setAllowBubbles(true);btnBubble.setOnClickListener(this);btnBubble2.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.btnBubble:Intent target = new Intent(MainActivity.this, BubbleActivity.class);PendingIntent bubbleIntent =PendingIntent.getActivity(MainActivity.this, 0, target, PendingIntent.FLAG_UPDATE_CURRENT /* flags */);// Create bubble metadataNotification.BubbleMetadata bubbleData =new Notification.BubbleMetadata.Builder().setDesiredHeight(600).setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher)).setIntent(bubbleIntent).build();builder = new Notification.Builder(MainActivity.this, channel.getId()).setSmallIcon(R.mipmap.ic_launcher).setBubbleMetadata(bubbleData);notificationManager.createNotificationChannel(channel);notificationManager.notify(1, builder.build());break;case R.id.btnBubble2:target = new Intent(MainActivity.this, BubbleActivity.class);target.putExtra("key","This is the second bubble");bubbleIntent =PendingIntent.getActivity(MainActivity.this, 0, target, PendingIntent.FLAG_UPDATE_CURRENT);// Create bubble metadatabubbleData = new Notification.BubbleMetadata.Builder().setDesiredHeight(600).setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher)).setIntent(bubbleIntent).build();builder = new Notification.Builder(MainActivity.this, channel.getId()).setContentTitle("Second Bubble").setSmallIcon(R.mipmap.ic_launcher).setBubbleMetadata(bubbleData);notificationManager.createNotificationChannel(channel);notificationManager.notify(2, builder.build());break;}}
}

Note: While writing this tutorial, the emulator is unable to display the Icons. This should be rectified in upcoming updates of Android Q after the Beta 2 soon.

注意:编写本教程时,仿真器无法显示图标。 Beta 2即将发布时,即将在Android Q中进行的更新应对此进行纠正。

We’ve created another activity using the Basic Activity template.

我们使用“基本活动”模板创建了另一个活动。

The code for the BubbleActivity.java is given below:

下面给出了BubbleActivity.java的代码:

package com.journaldev.androidqbubbles;import android.os.Bundle;import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.TextView;public class BubbleActivity extends AppCompatActivity {TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bubble);Toolbar toolbar = findViewById(R.id.toolbar);setSupportActionBar(toolbar);textView = findViewById(R.id.textView);FloatingActionButton fab = findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();}});}@Overrideprotected void onResume() {super.onResume();if (getIntent() != null && getIntent().getExtras() != null) {String value = getIntent().getStringExtra("key");textView.setText(value);}}
}

Here we update the TextView based on the PendingIntent results from the Notification.

在这里,我们根据通知的PendingIntent结果更新TextView。

Note: Do not forget to add the BubbleActivity to the Manifest with the correct attributes as discussed at the beginning of this tutorial.

注意:不要忘了将BubbleActivity添加到清单中,该属性具有本教程开头所讨论的正确属性。

The output of the above application in action is given below:

上面应用程序的输出如下:

Android Q Notification Bubble Output

Android Q Notification Bubble Output

Android Q通知气泡输出

As you can see, we are able to perform all actions in the BubbleActivity that’s displayed as a floating window.

如您所见,我们能够执行BubbleActivity中显示为浮动窗口的所有操作。

That brings an end to this tutorial. You can download the AndroidQBubbles tutorial from the link below or visit our Github Repository for the same.

这样就结束了本教程。 您可以从下面的链接下载AndroidQBubbles教程 ,或访问我们的Github存储库。

AndroidQBubblesAndroidQBubbles
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/28508/android-q-bubbles

安卓q bubbles

安卓q bubbles_Android Q Bubbles相关推荐

  1. 改革春风吹满地,安卓新系统Q上线腾讯WeTest

    作者:WeTest小编 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:wetest.qq.com/lab/view/44- "刚要适配安卓派,Q就来了." ...

  2. 你被大数据“杀熟”过吗?怎么解决的?丨Q言Q语

    点击关注 InfoQ,置顶公众号 接收程序员的技术早餐 网友"廖师傅廖师傅"表示,他经常通过某网站订某个特定酒店的房间,长年价格在 380 元 -400 元.偶然一次,他从前台得知 ...

  3. AngularJS $q 和 $q.all 单个数据源和多个数据源合并(promise的说明)

    这篇文章讲的不错, angular $q  和 promise!! -------------------------------------------------------------- 通过调 ...

  4. 身为程序员,你接过最奇葩的需求是什么?丨Q言Q语

    - Q 言 Q 语 第 二十一 期 - 本期话题: 身为程序员,你接过最奇葩的需求是什么? 身为执行部门,程序员们总是要去实现各种各样的需求,有的需求来自甲方,有的需求来自产品经理,还有的需求来自产品 ...

  5. 2018年,你想从InfoQ获取什么内容?丨Q言Q语

    - Q 言 Q 语 第 三 期 - Q言Q语是 InfoQ 推出的最新板块, 旨在给所有 InfoQer 一个展示观点的平台. 每期一个主题, 不扣帽子,不论对错,不看输赢, 只愿跟有趣的灵魂相遇. ...

  6. Linux vi种 wq 、wq!、x、q、q!区别

    上面的命令只是在vi编辑命令中使用 wq:表示保存退出 wq!:表示强制保存退出 x:表示保存退出 在vi/vim编辑中,直接按 x 可以删除当前光标所在处的字符. wq和wq!的区别如下: 有些文件 ...

  7. 【Linux】vi编辑器中:wq 、:wq!、:x、:q、:q!的详细区别

    文章目录 下面的命令只是在vi编辑命令中使用 `:wq`和`:wq!`的区别如下: `:x` 和 `:wq`的区别如下: `:q` 和 `:q!`的区别如下: 下面的命令只是在vi编辑命令中使用 :w ...

  8. Linux 中 wq 、wq!、x、q、q!区别

    wq:表示保存退出 wq!:表示强制保存退出 x:表示保存退出 wq和wq!的区别如下: 有些文件设置了只读,一般不是修改文件的,但是如果你是文件的owner或者root的话,通过wq!还是能保存文件 ...

  9. RFID 动态Q静态Q是什么意思

    RFID 动态Q静态Q是什么意思 前言 一.Q是什么? 二.Q的设置 1.Q的取值 2.动态Q值 3.静态Q值 总结 前言 在RFID读写器中,经常会发现在参数里面有一个参数设-Q,这个Q可不是常说的 ...

最新文章

  1. C++实现学生成绩管理系统
  2. 计算机软件集成项目工程师上海,2021年上海系统集成项目管理工程师报名时间和报名入口...
  3. 【git学习五】git基础之git分支
  4. 软考-信息系统项目管理师-项目组合管理
  5. ssh-add ssh-agent使用
  6. git本地创建新分支并推送到远程仓库
  7. php 获取 uri,获取URI地址
  8. Windows中MySQL主从数据库搭建(一)
  9. python循环语句c次_python循环语句
  10. quartus II 13.1 软件破解
  11. stm32毕业设计 太空游戏机设计与实现
  12. html制作dnf,DNF教你如何不花一分钱制作90顶级史诗
  13. 张磊:什么才是企业真正的护城河
  14. python + expect + list = goto myip
  15. qq邮箱不能上传文件的修复
  16. linux文件权限数字754,linux555、644、666、755、777权限详解数字代表什么意思
  17. mysql-8xxx遇坑后的安装
  18. 什么软件可以测试手长,心率检测专家-可以让你通过按压手指检测出心率的健康app...
  19. 北京大学可视化发展前沿研究生暑期学校Day6
  20. FFmpeg编译成Android动态库

热门文章

  1. nodejs+express开发blog(2)
  2. XCode6 ,iOS之PCH文件配置
  3. TeeChart的坐标轴
  4. oracle 锁表查询及解决、表字段查询
  5. [转载] python3 字符串比较函数_python3 字符串操作相关函数
  6. [转载] 在IPython中重新加载模块 importlib
  7. 开启和关闭oracle数据库中的审计功能
  8. Linux yum安装
  9. RTM-DSP项目总结
  10. js中去掉字符串中的某个指定字符