本文共 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 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:
应用程序快捷方式有两种:
ShortcutManager
API 动态快捷方式 –可以使用ShortcutManager
API动态创建这些ShortcutManager
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或更高版本。
Let’s look at the implementation of Static Shortcuts.
To create static shortcuts you must create an XML file with theshortcuts
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
下,如下所示:
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 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.
为此,我们需要ShortcutManager
和ShortcutInfo.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 ListstaticShortcuts = 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()
用于更改已定义的动态快捷方式。
getDynamicShortcuts()
to check for dynamic shortcuts every time. 每次启动应用程序时,都会检查动态快捷方式 -用户从备份中还原应用程序时,动态快捷方式不会保留。 因此,每次使用getDynamicShortcuts()
检查动态快捷方式是一个好习惯。 In the next section, we’ll implement the shortcuts in our Android Application.
在下一节中,我们将在Android应用程序中实现这些快捷方式。
The AndroidManifest.xml file looks like this:
AndroidManifest.xml文件如下所示:
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:
上面应用程序的输出如下:
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/