博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓q bubbles_Android Q Bubbles
阅读量:2532 次
发布时间:2019-05-11

本文共 9614 字,大约阅读时间需要 32 分钟。

安卓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 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:

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

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布局的代码:

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;    @Override    protected 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);    }    @Override    public 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 metadata                Notification.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 metadata                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())                        .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;    @Override    protected 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() {            @Override            public void onClick(View view) {                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });    }    @Override    protected 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存储库。

翻译自:

安卓q bubbles

转载地址:http://snlzd.baihongyu.com/

你可能感兴趣的文章
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>