第一步。在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="android.permission.INSTALL_SHORTCUT"/>
然后:添加meta-data:
<activity
android:name=".MainActivity"
android:exported="true">
<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>
第二步。在strings.xml文件添加:
<resources>
<string name="app_name">PracticeDemo</string>
<string name="static_disabled_message">点击无效</string>
<string name="static_shortcut_short_label">静态</string>
<string name="static_shortcut_long_label">MainActivity2</string>
<string name="dynamic_shortcut_short_label1">动态</string>
<string name="dynamic_shortcut_long_label1">百度搜索</string>
<string name="pinned_shortcut_short_label2">桌面</string>
<string name="pinned_shortcut_long_label2">桌面快捷方式</string>
</resources>
然后,在res/xml文件下新建一个shortcuts.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/map"
android:shortcutDisabledMessage="@string/static_disabled_message"
android:shortcutId="staticId"
android:shortcutLongLabel="@string/static_shortcut_long_label"
android:shortcutShortLabel="@string/static_shortcut_short_label"
>
<intent
android:action="android.intent.action.VIEW"
android:data="content://xiaweizi.com/fromStaticShortcut"
android:targetClass="com.application.practicedemo.MainActivity2"
android:targetPackage="com.application.practicedemo" />
<categories android:name="android.shortcut.conversation"
/>
</shortcut>
</shortcuts>
图片资源自己添加到res/drawable文件中啊!我的图片名字是:map.png
第三步。有个工具类Utils.java,代码如下:
package com.application.practicedemo;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
public class Utils {
private Utils() {
}
public static void showToast(final Context context, final String message) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
});
}
}
第四步。MainActivity.xml代码如下:
package com.application.practicedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
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.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String ID_DYNAMIC_1 = "id_dynamic_1";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 点击按钮后,再返回长按图标,右侧显示快捷方式-》百度搜索
setDynamicShortcuts();
// 创建桌面快捷方式
createPinnedShortcuts();
}
});
}
// 直达 “百度搜索 ”链接
private ShortcutInfo createShortcutInfo(){
return new ShortcutInfo.Builder(this, ID_DYNAMIC_1)
.setShortLabel(getString(R.string.dynamic_shortcut_short_label1))
.setLongLabel(getString(R.string.dynamic_shortcut_long_label1))
.setIcon(Icon.createWithResource(this, R.drawable.map))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")))
.build();
}
private void setDynamicShortcuts() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfo = new ArrayList<>();
shortcutInfo.add(createShortcutInfo());
if (shortcutManager != null) {
shortcutManager.setDynamicShortcuts(shortcutInfo);
}
}
}
//创建桌面快捷方式,可单独显示
private void createPinnedShortcuts() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
Intent intent = new Intent(this, MainActivity2.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("key", "fromPinnedShortcut");
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, "my-shortcut")
.setShortLabel(getString(R.string.pinned_shortcut_short_label2))
.setLongLabel(getString(R.string.pinned_shortcut_long_label2))
.setIcon(Icon.createWithResource(this, R.drawable.map))
.setIntent(intent)
.build();
Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
PendingIntent successCallback = PendingIntent.getActivity(this, 0, pinnedShortcutCallbackIntent, PendingIntent.FLAG_IMMUTABLE);
boolean b = shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
Utils.showToast(this, "set pinned shortcuts " + (b ? "success" : "failed") + "!");
}
}
}
}
布局代码如下,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第五步。新建一个MainActivity2.java,代码如下:
package com.application.practicedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
布局代码如下:activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#4CAF50"
tools:context=".MainActivity2">
<TextView
android:layout_gravity="center_horizontal"
android:text="MainActivity2"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
最后的效果如下:
长按图标,弹出快捷入口;
点击按钮,创建桌面快捷方式。