Android中的长按快捷方式实现

快捷方式

Android 7.1 (API level 25)以上才支持

1. 三种类型

  • 静态类型。这样实现的话当需要更新快捷方式的时候必须更新整个APP
  • 动态类型。这样可以在APP运行的时候利用ShortcutManager进行添加、更新或移除
  • 桌面类型。这种也是能在运行时添加,也是利用ShortcutManager进行操作。但是需要用户同意才行

2. 静态快捷方式的使用

1. 在AndroidManifest.xml中找到主Activity。即包含android.intent.action.MAINandroid.intent.category.LAUNCHER的。

2. 添加<meta-data>节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.changqin.androidfuturejava"> <application android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> <meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
</manifest>

3. 创建res/xml/shortcuts.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut android:shortcutId="compose"
android:enabled="true"
android:icon="@drawable/shortcuts_add"
android:shortcutShortLabel="@string/test1"
android:shortcutLongLabel="@string/test2"
android:shortcutDisabledMessage="@string/test3">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="com.changqin.androidfuturejava"
android:targetClass="com.changqin.androidfuturejava.shortcut.ShortTestActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when they launch this shortcut. --> <categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. --> </shortcuts>
  • shortcutShortLabel指的是快捷方式的简明短语
  • shortcutLongLabel指的是扩展短语,如果有足够的空间,APP将显示这个值而不是shortcutShortLabel
  • shortcutDisabledMessage指的是当用户试图启动禁用的快捷方式时,APP中出现的消息。这个消息应该向用户解释为什么现在禁用了快捷方式。如果android:enabledtrue,则该属性的值没有影响。
  • android:shortcutShortLabelandroid:shortcutLongLabelandroid:shortcutDisabledMessage必须使用@string/shortcut_short_label格式

正常显示的快捷方式

3. 使用动态快捷方式

动态快捷方式应该为应用程序中特定的、上下文敏感的操作提供actions。这些actions可以在应用程序的使用之间进行更改,甚至在应用程序运行时也可以进行更改。
ShortcutManager允许我们在动态快捷方式中做以下操作

  • 添加: 使用setDynamicShortcuts()来重新添加整个快捷方列表
  • 更新 使用updateShortcuts()来更新
  • 移除 使用removeDynamicShortcuts()来移除一组,或者使用removeAllDynamicShortcuts()来移除所有的快捷方式
1
2
3
4
5
6
7
8
9
10
@TargetApi(Build.VERSION_CODES.N_MR1)
public void addShort(View view) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("Web site")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(this, R.drawable.ic_add_white_24dp))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://changqin.win")))
.build(); shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut)); Toast.makeText(this,"add success",Toast.LENGTH_SHORT).show(); }

4. 使用桌面快捷方式

在Android 8.0 (API level 26)或以上,我们可以创建桌面快捷方式。不同于前两者,桌面快捷方式允许有自己独立的icon。

创建步骤

  1. 使用isRequestPinShortcutSupported()来验证设备是否支持在APP上创建快捷方式。
  2. 创建ShortcutInfo对象。
    a. 如果对象已存在,只需创建包含ID的对象即可。系统自动查找与快捷方式相关的所有信息。
    b. 如果是一个新的对象,那么创建的时候必须包含IDintentshort label
  3. 调用requestPinShortcut()请求添加桌面快捷方式。在此期间,可以通过PendingIntent对象来监听快捷方式是否添加成功。ps: 如果用户不允许添加,是不会收到回调的。当快捷方式添加成功后,可以用updateShortcuts()来更新。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RequiresApi(api = Build.VERSION_CODES.O)
public void addDesktopShort(View view) {
if (mShortcutManager.isRequestPinShortcutSupported()) {
// Assumes there's already a shortcut with the ID "my-shortcut".
// The shortcut must be enabled. ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(this, "my-shortcut")
.build(); // Create the PendingIntent object only if your app needs to be notified
// that the user allowed the shortcut to be pinned. Note that, if the // pinning operation fails, your app isn't notified. We assume here that the // app has implemented a method called createShortcutResultIntent() that // returns a broadcast intent. Intent pinnedShortcutCallbackIntent =
mShortcutManager.createShortcutResultIntent(pinShortcutInfo); // Configure the intent so that your app's broadcast receiver gets
// the callback successfully. PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
pinnedShortcutCallbackIntent, 0); mShortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
}
}