博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
捷径app 保存视频_Android N App捷径
阅读量:2531 次
发布时间:2019-05-11

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

捷径app 保存视频

Android N brought App Shortcuts for developers and users. In this tutorial, we’ll be creating a sample application using App Shortcuts.

Android N为开发人员和用户带来了应用程序快捷方式。 在本教程中,我们将使用“应用程序快捷方式”创建一个示例应用程序。

应用程式捷径 (App Shortcuts)

App Shortcuts are designed to do common actions in the application from the launcher screen.

We can reach certain screens by just long pressing the app icon and clicking the respective shortcut.
As of Android N, each application can display a maximum of 5 shortcuts.

应用程序快捷方式旨在通过启动器屏幕在应用程序中执行常见操作。

只需长按应用程序图标并单击相应的快捷方式,便可以进入某些屏幕。
从Android N开始,每个应用程序最多可以显示5个快捷键。

App Shortcuts are of two types:

应用程序快捷方式有两种:

  • Static Shortcuts – These are defined in the resource file. To change them you must re-deploy your application

    静态快捷方式 –在资源文件中定义。 要更改它们,您必须重新部署您的应用程序
  • Dynamic Shortcuts – These can be created dynamically using ShortcutManager API

    动态快捷方式 –可以使用ShortcutManager API动态创建这些ShortcutManager
You can keep a check of the most visited screens in your app and accordingly create dynamic shortcuts for them.
您可以检查应用程序中访问量最大的屏幕,并相应地为其创建动态快捷方式。

Some come use cases of App Shortcuts are, sending an email, Booking cab/Ordering Food, Redialling a number etc.

应用快捷方式的一些用例包括:发送电子邮件,预订出租车/订购食品,重拨号码等。

To implement App Shortcuts, you must use Android SDK 25 or above.

要实施应用程序快捷方式,您必须使用Android SDK 25或更高版本。

静态快捷方式 (Static Shortcuts)

Let’s look at the implementation of Static Shortcuts.

To create static shortcuts you must create an XML file with the shortcuts tag as the root one.

让我们看一下静态快捷方式的实现。

要创建静态快捷方式,您必须创建一个XML文件,并将shortcuts标签作为根文件。

Your static_shortcuts.xml will be under xml under res folder and will look like this:

您的static_shortcuts.xml将位于res文件夹下的xml下,如下所示:

By default, when you launch an activity from the shortcut intent and press back, the application returns to the home launcher. If you want to return to the previous screen in the navigation flow, you can specify multiple intents as we did in the second shortcut in the snippet above.
默认情况下,当您从快捷方式意图启动活动并按返回时,该应用程序将返回到主启动器。 如果您想返回到导航流中的上一个屏幕,则可以像在上面片段的第二个快捷方式中那样指定多个意图。

Following are some of the inferences that we can draw from the above snippet:

以下是我们可以从以上代码段中得出的一些推论:

  • android:shortcutId – Refers to the unique identifer of the shortcut.

    android:shortcutId –指代android:shortcutId的唯一标识符。
  • shortcutShortLabel – Here we set the string to be displayed in the shortcut. This contains the shorthand form. For the long string, we use android:shortcutLongLabel

    shortcutShortLabel –在这里,我们设置要在快捷方式中显示的字符串。 这包含简写形式。 对于长字符串,我们使用android:shortcutLongLabel
  • android:icon – Here we set the icon which is shown on the left of the shortcut.

    android:icon –在这里我们设置显示在快捷方式左侧的图标。
  • intent – Here we pass on the intent for the shortcut. Inside the intent, we must set the action, target class and target package names, all fully qualified paths.

    intent –在这里,我们传递快捷方式的目的。 在意图内部,我们必须设置动作,目标类和目标程序包名称以及所有完全限定的路径。
  • extras – This goes inside the intent tag. We can pass Bundle extras here.

    extras –位于intent标记内。 我们可以在此处通过捆绑包附加服务。

In order to integrate the above static shortcuts in our application, we must set them in the launcher activity meta-data tag.

为了将上述静态快捷方式集成到我们的应用程序中,我们必须在启动器活动元数据标记中进行设置。

动态快捷方式 (Dynamic Shortcuts)

Dynamic Shortcuts can be created, removed, updated at runtime without the need to re-deploy the application.

动态快捷方式可以在运行时创建,删除,更新,而无需重新部署应用程序。

For this, we need the ShortcutManager and ShortcutInfo.Builder to create individual shortcuts.

为此,我们需要ShortcutManagerShortcutInfo.Builder创建单独的快捷方式。

ShortcutManager can be created in activity since it uses the getSystemService.

由于ShortcutManager使用getSystemService因此可以在活动中创建它。

The following code gets all the static and dynamic shortcuts from the shortcut manager:

以下代码从快捷方式管理器获取所有静态和动态快捷方式:

// Get all static shortcuts List
staticShortcuts = shortcutManager.getManifestShortcuts();// Get all dynamic shortcuts List
dynamicShortcuts = shortcutManager.getDynamicShortcuts();

setDynamicShortcuts is used to set the collection of dynamic shortcuts (ShorcutInfo) defined on the ShortcutManager instance

setDynamicShortcuts用于设置在ShortcutManager实例上定义的动态快捷方式( ShorcutInfo )的集合

updateDynamicShortcuts() is used to change already defined dynamic shortcut(s).

updateDynamicShortcuts()用于更改已定义的动态快捷方式。

捷径最佳做法 (Best Practices for Shortcuts)

  • Don’t use more than 5 shortcuts at a time – Typically, most launchers can’t display more than 4 shortcuts at a time.

    一次不要使用5个以上的快捷键 -通常,大多数启动器一次不能显示4个以上的快捷键。
  • Check Dynamic Shortcuts exists every time you launch the app – Dynamic shortcuts aren’t preserved when a user restores your application from a backup. Hence it is a good practice to use getDynamicShortcuts() to check for dynamic shortcuts every time.

    每次启动应用程序时,都会检查动态快捷方式 -用户从备份中还原应用程序时,动态快捷方式不会保留。 因此,每次使用getDynamicShortcuts()检查动态快捷方式是一个好习惯。
  • Maintain short label names – Generally, the short length of shortcut labels is 10 characters to keep the name of the shortcut as short as possible.

    保持简短的标签名称 –快捷标签的短长度通常为10个字符,以使快捷名称尽可能短。

In the next section, we’ll implement the shortcuts in our Android Application.

在下一节中,我们将在Android应用程序中实现这些快捷方式。

项目结构 (Project Structure)

Android N App Shortcuts Project

Android N App Shortcuts Project

Android N App快捷方式项目

(Code)

The AndroidManifest.xml file looks like this:

AndroidManifest.xml文件如下所示:

Android N App Shortcuts Manifest

Android N App Shortcuts Manifest

Android N App快捷方式清单

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.androidnappshortcuts;import android.content.Intent;import android.content.pm.ShortcutInfo;import android.content.pm.ShortcutManager;import android.graphics.drawable.Icon;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import java.util.Arrays;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    Button btnDynamicShortcut, btnRemoveShortcut, btnNext;    String ACTION_KEY = "anything.any";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnDynamicShortcut = findViewById(R.id.btnDynamicShortcut);        btnRemoveShortcut = findViewById(R.id.btnRemoveShortcut);        btnNext = findViewById(R.id.btnNext);        btnNext.setOnClickListener(this);        btnDynamicShortcut.setOnClickListener(this);        btnRemoveShortcut.setOnClickListener(this);        if (ACTION_KEY.equals(getIntent().getAction())){            Toast.makeText(getApplicationContext(),"First Dynamic Shortcut clicked",Toast.LENGTH_LONG).show();        }    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btnNext:                startActivity(new Intent(MainActivity.this, StaticShortcutActivity.class));                break;            case R.id.btnDynamicShortcut:                createSimpleDynamicShortcut();                break;            case R.id.btnRemoveShortcut:                removeShortcuts();                break;        }    }    private void createSimpleDynamicShortcut() {        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);        Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);        intent1.setAction(ACTION_KEY);        ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "dShortcut1")                .setIntent(intent1)                .setRank(1)                .setLongLabel("Dynamic Shortcut 1")                .setShortLabel("This is the shortcut 1")                .setIcon(Icon.createWithResource(this, R.drawable.ic_home_black_24dp))                .build();        ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "web_link")                .setRank(0)                .setShortLabel("Journaldev.com")                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com")))                .build();        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut1, shortcut2));        //shortcutManager.disableShortcuts(Arrays.asList(shortcut1.getId()));    }    private void removeShortcuts() {        ShortcutManager manager = getSystemService(ShortcutManager.class);        manager.removeAllDynamicShortcuts();    }}

setRank() is used to order the dynamic shortcuts in the shortcuts pane.

setRank()用于在快捷方式窗格中对动态快捷方式进行排序。

The code for the StaticShortcutActivity.java is given below:

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

package com.journaldev.androidnappshortcuts;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.TextView;public class StaticShortcutActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_static_shortcut);        Toolbar toolbar = findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        TextView textView = findViewById(R.id.tvTitle);        if (getIntent() != null && getIntent().getStringExtra("key") != null) {            String bundleString = getIntent().getStringExtra("key");            textView.setText(bundleString);        }        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();            }        });        getSupportActionBar().setDisplayHomeAsUpEnabled(true);    }}

You can get the layout of the above activity from the source code at the end of this tutorial.

您可以从本教程结尾的源代码中获取上述活动的布局。

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

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

Android N App Shortcuts Output

Android N App Shortcuts Output

Android N App快捷方式输出

That’s a wrap up for this tutorial. You can download the project from the link below or always browse the full source code in our Github Repository.

这是本教程的总结。 您可以从下面的链接下载项目,或者始终在我们的Github存储库中浏览完整的源代码。

翻译自:

捷径app 保存视频

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

你可能感兴趣的文章
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>