捷径app 保存视频_Android N App捷径

捷径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下,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="https://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_home_black_24dp"
        android:shortcutId="main_activity"
        android:shortcutLongLabel="@string/shortcut1_long"
        android:shortcutShortLabel="@string/shortcut1_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.journaldev.androidnappshortcuts.MainActivity"
            android:targetPackage="com.journaldev.androidnappshortcuts"/>
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_important_devices_black_24dp"
        android:shortcutDisabledMessage="@string/disabled_msg"
        android:shortcutId="SecondActivity"
        android:shortcutLongLabel="@string/shortcut2_long"
        android:shortcutShortLabel="@string/shortcut2_short">

        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="com.journaldev.androidnappshortcuts.MainActivity"
            android:targetPackage="com.journaldev.androidnappshortcuts" />

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.journaldev.androidnappshortcuts.StaticShortcutActivity"
            android:targetPackage="com.journaldev.androidnappshortcuts">

            <extra
                android:name="key"
                android:value="Static Shortcut 2 Extras Message" />

        </intent>
    </shortcut>


</shortcuts>
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.

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

<meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/static_shortcuts" />

动态快捷方式 (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<ShortcutInfo> staticShortcuts = shortcutManager.getManifestShortcuts();

// Get all dynamic shortcuts 
List<ShortcutInfo> 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布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home Screen" />


    <Button
        android:id="@+id/btnNext"
        android:text="Second Screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnDynamicShortcut"
        android:text="Create Dynamic Shortcuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/btnRemoveShortcut"
        android:text="Remove Dynamic Shortcuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</LinearLayout>

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存储库中浏览完整的源代码。

翻译自: https://www.journaldev.com/28529/android-n-app-shortcuts

捷径app 保存视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值