Android N App捷径
Android N为开发人员和用户带来了应用程序快捷方式。
在本教程中,我们将使用"应用程序快捷方式"创建一个示例应用程序。
应用程式捷径
应用程序快捷方式旨在在启动器屏幕上执行应用程序中的常见操作。
只需长按应用程序图标并单击相应的快捷方式,便可以进入某些屏幕。
从Android N开始,每个应用程序最多可以显示5个快捷键。
应用程序快捷方式有两种:
- 静态快捷方式–在资源文件中定义。
要更改它们,您必须重新部署您的应用程序 - 动态快捷方式–可以使用
ShortcutManagerAPI动态创建这些快捷方式
您可以检查应用程序中访问量最大的屏幕,从而为它们创建动态快捷方式。
应用快捷方式的一些用例包括:发送电子邮件,预订出租车/订购食品,重拨号码等。
要实施应用程序快捷方式,您必须使用Android SDK 25或者更高版本。
静态快捷方式
让我们看一下静态快捷方式的实现。
要创建静态快捷方式,您必须创建一个XML文件,并以shortcuts标记为根。
您的" 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.theitroad.androidnappshortcuts.MainActivity"
android:targetPackage="com.theitroad.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.theitroad.androidnappshortcuts.MainActivity"
android:targetPackage="com.theitroad.androidnappshortcuts"
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.theitroad.androidnappshortcuts.StaticShortcutActivity"
android:targetPackage="com.theitroad.androidnappshortcuts">
<extra
android:name="key"
android:value="Static Shortcut 2 Extras Message"
</intent>
</shortcut>
</shortcuts>
默认情况下,当您从快捷方式意图启动活动并按返回时,应用程序将返回到主启动器。
如果您想返回到导航流中的上一个屏幕,则可以像上面片段中的第二个快捷方式一样指定多个意图。
以下是我们可以从以上代码段中得出的一些推论:
android:shortcutId-指快捷方式的唯一标识。
shortcutShortLabel–这里我们设置要在快捷方式中显示的字符串。
这包含简写形式。
对于长字符串,我们使用ʻandroid:shortcutLongLabel`android:icon –在这里我们设置图标,该图标显示在快捷方式的左侧。
"意图" –这里我们传递了快捷方式的意图。
在意图内部,我们必须设置动作,目标类和目标程序包名称,以及所有完全限定的路径。extras–这在intent标记内。
我们可以在此处通过捆绑包附加服务。
为了将上述静态快捷方式集成到我们的应用程序中,我们必须在启动器活动元数据标记中进行设置。
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/static_shortcuts"
动态快捷方式
动态快捷方式可以在运行时创建,删除,更新,而无需重新部署应用程序。
为此,我们需要ShortcutManager和ShortcutInfo.Builder来创建单独的快捷方式。
因为ShortcutManager使用getSystemService,所以可以在活动中创建它。
以下代码从快捷方式管理器获取所有静态和动态快捷方式:
//Get all static shortcuts List<ShortcutInfo> staticShortcuts = shortcutManager.getManifestShortcuts(); //Get all dynamic shortcuts List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
setDynamicShortcuts用于设置在ShortcutManager实例上定义的动态快捷方式(ShorcutInfo)的集合。
updateDynamicShortcuts()用于更改已定义的动态快捷方式。
捷径最佳做法
一次不要使用5个以上的快捷键-通常,大多数启动器一次不能显示4个以上的快捷键。
每次启动应用程序时,都会检查动态快捷方式-用户从备份中还原应用程序时,动态快捷方式不会保留。
因此,每次使用getDynamicShortcuts()来检查动态快捷方式是一个好习惯。保留简短的标签名称–快捷标签的短长度通常为10个字符,以使快捷名称尽可能短。
在下一部分中,我们将在Android应用程序中实现这些快捷方式。
代码
AndroidManifest.xml文件如下所示:
Android N App快捷方式列表
下面给出了" 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>
MainActivity.java类的代码如下:
package com.theitroad.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("theitroad.local")
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.theitroad.local")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut1, shortcut2));
//shortcutManager.disableShortcuts(Arrays.asList(shortcut1.getId()));
}
private void removeShortcuts() {
ShortcutManager manager = getSystemService(ShortcutManager.class);
manager.removeAllDynamicShortcuts();
}
}
setRank()用于在快捷方式窗格中对动态快捷方式进行排序。
下面给出了" StaticShortcutActivity.java"的代码:
package com.theitroad.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);
}
}
您可以从本教程末尾的源代码中获取上述活动的布局。

