亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 桌面快捷方式實現(xiàn)實例詳解

 更新時間:2022年11月27日 16:53:58   作者:ChenYhong  
這篇文章主要為大家介紹了Android 桌面快捷方式實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Shortcuts API 簡介

快捷方式在各類App中已經(jīng)十分常見,快捷方式可以讓用戶直達想要使用的功能,例如快速打開掃一掃、快速打開健康碼等。對此,Android提供了Shortcuts API,本文介紹如何使用Shortcuts API來實現(xiàn)快捷方式。

在Android中,快捷方式通常顯示在App的啟動器或支持的助理中(例如Google 助理)。每個快捷方式對應一個或者多個Intent,當用戶選中某個快捷方式時,系統(tǒng)會啟動對應的Intent。

需要注意:

  • 只有包含<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />Activity(即啟動頁)可以擁有快捷方式。
  • 不同設(shè)備支持的快捷方式數(shù)量可能不同,通常每個啟動器最多顯示4個快捷方式,可以通過ShortcutManagerCompat.getMaxShortcutCountPerActivity(context)來獲取設(shè)備支持的快捷方式數(shù)量。

Android 支持下列三種快捷方式:

  • 靜態(tài)快捷方式:通過xml配置生成。
  • 動態(tài)快捷方式:在運行時通過代碼配置生成、更新和移除。
  • 桌面快捷方式:在運行時通過代碼配置生成、更新,需要用戶同意后才能添加。

下面分別介紹這三種類型的快捷方式如何實現(xiàn)。

靜態(tài)快捷方式

快捷方式配置

在res/xml目錄下創(chuàng)建shortcuts.xml,如下圖:

注意,此方式僅在Android N_MR1(25)以上可用。

文件內(nèi)容如下:

<?xml version ="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon_biometrics"
        android:shortcutDisabledMessage="@string/shortcuts_disable_message"
        android:shortcutId="biometrics"
        android:shortcutLongLabel="@string/biometrics_shortcuts_long_label"
        android:shortcutShortLabel="@string/biometrics_shortcuts_short_label">
        <!--快捷方式跳轉(zhuǎn)的頁面,必須配置-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.androidapi.biometrics.BiometricActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
    </shortcut>
</shortcuts>

配置文件中,每個shortcut標簽必須配置的值為android:shortcutIdandroid:shortcutShortLabel,其余為可選配置。

屬性名屬性值
android:shortcutIdid, 不能設(shè)置為字符串資源,必須配置
android:shortcutShortLabel簡述,建議限制在10個字符以內(nèi),字符串資源,必須配置
android:shortcutLongLabel詳細描述,顯示空間足夠大時會顯示此值,建議限制在25個字符內(nèi),字符串資源,非必須
android:icon圖標,圖片的路徑或圖片資源文件,非必須
android:enabled是否可用,布爾值,非必須
android:shortcutDisabledMessage用戶點擊不可用的快捷方式時的提示語,僅在enable為false時生效,字符串資源,非必須

在Manifest中添加快捷方式配置

AndroidManifest中啟動頁標簽下添加meta-data,如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chenyihong.exampledemo">
    <application
        ....
        >
        <activity
            android:name=".home.HomeActivity"
            android:exported="true"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>
</manifest>

效果如圖:

動態(tài)快捷方式

可以在運行時通過代碼添加或移除動態(tài)快捷方式,代碼如下:

class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: LayoutShortcutsActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_shortcuts_activity)
        binding.includeTitle.tvTitle.text = "Shortcuts Api"
        binding.btnCreateShortcut.setOnClickListener {
            // 創(chuàng)建動態(tài)快捷方式
            createDynamicShortcuts()
        }
        binding.btnRemoveShortcut.setOnClickListener {
            // 根據(jù)ID移除指定的動態(tài)快捷方式
            ShortcutManagerCompat.removeDynamicShortcuts(this, arrayListOf(locationShortcutId))
            // 移除所有的動態(tài)快捷方式
            // ShortcutManagerCompat.removeAllDynamicShortcuts(this)
        }
    }
    private fun createDynamicShortcuts() {
        val dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this)
        val locationShortcut = ShortcutInfoCompat.Builder(this, locationShortcutId)
            .setShortLabel(getString(R.string.location_shortcuts_short_label))
            .setLongLabel(getString(R.string.location_shortcuts_long_label))
            .setDisabledMessage(getString(R.string.shortcuts_disable_message))
            .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
            .setIntent(Intent(Intent.ACTION_VIEW).apply {
                component = ComponentName(packageName, GpsSignalActivity::class.java.name)
            })
            .build()
        if (dynamicShortcuts.isEmpty() || !dynamicShortcuts.contains(locationShortcut)) {
            ShortcutManagerCompat.pushDynamicShortcut(this, locationShortcut)
        }
    }
}

效果如圖:

桌面快捷方式

運行時創(chuàng)建

桌面快捷方式也十分常見,例如支付寶可以將掃一掃、乘車碼等固定到桌面??梢酝ㄟ^如下代碼添加桌面快捷方式:

class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        binding.btnCreatePinShortcut.setOnClickListener {
            // 創(chuàng)建桌面快捷方式
            createPinShortcuts()
        }
    }
    private fun createPinShortcuts() {
        // 先判斷是否支持添加桌面快捷方式
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            val pinShortcutInfo = ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(getString(R.string.location_shortcuts_short_label))
                .setLongLabel(getString(R.string.location_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
            val pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, pinShortcutInfo)
            val successCallback = PendingIntent.getBroadcast(this, 0, pinnedShortcutCallbackIntent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0)
            ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo, successCallback.intentSender)
        }
    }
}

效果如圖:

支持用戶主動創(chuàng)建

可以通過配置創(chuàng)建快捷方式專用的Activity來支持用戶主動創(chuàng)建桌面快捷方式,代碼如下:

class CreateCameraShortcutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: LayoutCreateShortcutsActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_create_shortcuts_activity)
        binding.tvTips.text = "Do you want to add the Camera Launcher icon to your home screen?"
        binding.btnAddShortcut.setOnClickListener {
            createPinShortcuts()
        }
        binding.btnReject.setOnClickListener {
            finish()
        }
    }
    private fun createPinShortcuts() {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "camera")
                .setShortLabel(getString(R.string.camera_shortcuts_short_label))
                .setLongLabel(getString(R.string.camera_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_camera))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, CameraActivity::class.java.name)
                })
                .build()
            val pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, pinShortcutInfo)
            setResult(RESULT_OK, pinnedShortcutCallbackIntent)
            finish()
        }
    }
}
// 在 manifest中配置activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chenyihong.exampledemo">
    <application
        ...
        >
        <activity
            android:name=".androidapi.shortcuts.CreateCameraShortcutActivity"
            android:exported="true"
            android:icon="@drawable/icon_camera"
            android:label="@string/label_camera"
            android:theme="@style/DialogActivity">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

效果如圖:

打開多個Activity

每個快捷方式都可以配置多個Intent,當用戶選中此快捷方式時,會鏈式的打開所有Intent對應的頁面,代碼如下:

// 靜態(tài)快捷方式
<?xml version ="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon_search_48"
        android:shortcutDisabledMessage="@string/shortcuts_disable_message"
        android:shortcutId="search"
        android:shortcutLongLabel="@string/search_shortcuts_long_label"
        android:shortcutShortLabel="@string/search_shortcuts_short_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.home.HomeActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.androidapi.search.SearchActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
    </shortcut>
</shortcuts>
// 動態(tài)快捷方式(桌面快捷方式與此類似)
class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        binding.btnCreateMultipleIntentsShortcut.setOnClickListener {
            createMultipleIntentsDynamicShortcuts()
        }
    }
    private fun createMultipleIntentsDynamicShortcuts() {
        val dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this)
        val locationShortcut = ShortcutInfoCompat.Builder(this, locationShortcutId)
            .setShortLabel(getString(R.string.location_shortcuts_short_label))
            .setLongLabel(getString(R.string.location_shortcuts_long_label))
            .setDisabledMessage(getString(R.string.shortcuts_disable_message))
            .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
            .setIntents(arrayOf(
                Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, HomeActivity::class.java.name)
                },
                Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                }))
            .build()
        if (dynamicShortcuts.isEmpty() || !dynamicShortcuts.contains(locationShortcut)) {
            ShortcutManagerCompat.pushDynamicShortcut(this, locationShortcut)
        }
    }
}

效果如圖:

更新快捷方式

啟用和禁用

在需要的時候,例如當定位不可用時禁止定位快捷方式,定位恢復可用時重新啟用快捷方式,代碼如下:

class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        binding.btnEnableShortcut.setOnClickListener {
            // 啟用快捷方式
            ShortcutManagerCompat.enableShortcuts(this, arrayListOf(ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(getString(R.string.location_shortcuts_short_label))
                .setLongLabel(getString(R.string.location_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
            ))
        }
        binding.btnDisableShortcut.setOnClickListener {
            // 禁用快捷方式,需要傳入禁用原因,當用戶點擊時會顯示
            ShortcutManagerCompat.disableShortcuts(this, arrayListOf(locationShortcutId), "Location function is currently unavailable")
        }
    }
}

效果如圖:

注意,根據(jù)效果圖可以看到,禁用后動態(tài)快捷方式會被移除,再次啟用后不會自動添加,需要手動添加。

更新快捷方式的樣式

在需要的時候,例如用戶切換語言時可以更新快捷方式的樣式,顯示匹配當前語言的文案,代碼如下:

class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    private var english = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        binding.btnUpdateShortcut.setOnClickListener {
            updateDynamicShortcuts()
        }
    }
    private fun updateDynamicShortcuts() {
        english = !english
        ShortcutManagerCompat.updateShortcuts(this, arrayListOf(
            ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(if (english) getString(R.string.location_shortcuts_short_label) else "使用定位")
                .setLongLabel(if (english) getString(R.string.location_shortcuts_long_label) else "通過定位獲取位置信息")
                .setDisabledMessage(if (english) getString(R.string.shortcuts_disable_message) else "此快捷方式不可用")
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
        ))
    }
}

效果如圖:

注意,通過代碼只能更新動態(tài)快捷方式和桌面快捷方式,如果更新了靜態(tài)快捷方式會崩潰。

示例

整合之后做了個示例Demo,代碼如下:

// 快捷方式配置文件
<?xml version ="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon_biometrics"
        android:shortcutDisabledMessage="@string/shortcuts_disable_message"
        android:shortcutId="biometrics"
        android:shortcutLongLabel="@string/biometrics_shortcuts_long_label"
        android:shortcutShortLabel="@string/biometrics_shortcuts_short_label">
        <!--快捷方式跳轉(zhuǎn)的頁面,必須配置-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.androidapi.biometrics.BiometricActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon_search_48"
        android:shortcutDisabledMessage="@string/shortcuts_disable_message"
        android:shortcutId="search"
        android:shortcutLongLabel="@string/search_shortcuts_long_label"
        android:shortcutShortLabel="@string/search_shortcuts_short_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.home.HomeActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.chenyihong.exampledemo.androidapi.search.SearchActivity"
            android:targetPackage="com.chenyihong.exampledemo" />
    </shortcut>
</shortcuts>
// 彈窗樣式的Activity Style
<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="DialogActivity" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- 背景色透明度 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 無標題 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 半透明,設(shè)置為false無透明效果 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 窗口樣式Dialog -->
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    </style>
</resources>
// Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chenyihong.exampledemo">
    <application
        ...
        >
        <activity
            android:name=".home.HomeActivity"
            android:exported="true"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity
            android:name=".androidapi.shortcuts.ShortcutsActivity"
            android:exported="false"
            android:screenOrientation="portrait" />
        <activity
            android:name=".androidapi.shortcuts.CreateCameraShortcutActivity"
            android:exported="true"
            android:icon="@drawable/icon_camera"
            android:label="@string/label_camera"
            android:theme="@style/DialogActivity">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".androidapi.shortcuts.CreateLocationShortcutActivity"
            android:exported="true"
            android:icon="@drawable/icon_location"
            android:label="@string/label_location"
            android:theme="@style/DialogActivity">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>
    </application>
</manifest>
// 示例Activity
class ShortcutsActivity : AppCompatActivity() {
    private val locationShortcutId = "location"
    private var english = true
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: LayoutShortcutsActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_shortcuts_activity)
        binding.includeTitle.tvTitle.text = "Shortcuts Api"
        binding.btnCreateShortcut.setOnClickListener {
            // 創(chuàng)建動態(tài)快捷方式
            createDynamicShortcuts()
        }
        binding.btnRemoveShortcut.setOnClickListener {
            // 根據(jù)ID移除指定的動態(tài)快捷方式
            ShortcutManagerCompat.removeDynamicShortcuts(this, arrayListOf(locationShortcutId))
            // 根據(jù)ID移除指定的動態(tài)快捷方式
            // ShortcutManagerCompat.removeAllDynamicShortcuts(this)
        }
        binding.btnCreatePinShortcut.setOnClickListener {
            // 創(chuàng)建桌面快捷方式
            createPinShortcuts()
        }
        binding.btnCreateMultipleIntentsShortcut.setOnClickListener {
            createMultipleIntentsDynamicShortcuts()
        }
        binding.btnEnableShortcut.setOnClickListener {
            // 啟用快捷方式
            ShortcutManagerCompat.enableShortcuts(this, arrayListOf(ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(getString(R.string.location_shortcuts_short_label))
                .setLongLabel(getString(R.string.location_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
            ))
        }
        binding.btnDisableShortcut.setOnClickListener {
            // 禁用快捷方式,需要傳入禁用原因,當用戶點擊時會顯示
            ShortcutManagerCompat.disableShortcuts(this, arrayListOf(locationShortcutId), "Location function is currently unavailable")
        }
        binding.btnUpdateShortcut.setOnClickListener {
            // 更新快捷方式
            updateDynamicShortcuts()
        }
    }
    private fun createDynamicShortcuts() {
        val dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this)
        val locationShortcut = ShortcutInfoCompat.Builder(this, locationShortcutId)
            .setShortLabel(getString(R.string.location_shortcuts_short_label))
            .setLongLabel(getString(R.string.location_shortcuts_long_label))
            .setDisabledMessage(getString(R.string.shortcuts_disable_message))
            .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
            .setIntent(Intent(Intent.ACTION_VIEW).apply {
                component = ComponentName(packageName, GpsSignalActivity::class.java.name)
            })
            .build()
        if (dynamicShortcuts.isEmpty() || !dynamicShortcuts.contains(locationShortcut)) {
            ShortcutManagerCompat.pushDynamicShortcut(this, locationShortcut)
        }
    }
    private fun createMultipleIntentsDynamicShortcuts() {
        val dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this)
        val locationShortcut = ShortcutInfoCompat.Builder(this, locationShortcutId)
            .setShortLabel(getString(R.string.location_shortcuts_short_label))
            .setLongLabel(getString(R.string.location_shortcuts_long_label))
            .setDisabledMessage(getString(R.string.shortcuts_disable_message))
            .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
            .setIntents(arrayOf(
                Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, HomeActivity::class.java.name)
                },
                Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                }))
            .build()
        if (dynamicShortcuts.isEmpty() || !dynamicShortcuts.contains(locationShortcut)) {
            ShortcutManagerCompat.pushDynamicShortcut(this, locationShortcut)
        }
    }
    private fun updateDynamicShortcuts() {
        english = !english
        ShortcutManagerCompat.updateShortcuts(this, arrayListOf(
            ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(if (english) getString(R.string.location_shortcuts_short_label) else "使用定位")
                .setLongLabel(if (english) getString(R.string.location_shortcuts_long_label) else "通過定位獲取位置信息")
                .setDisabledMessage(if (english) getString(R.string.shortcuts_disable_message) else "此快捷方式不可用")
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
        ))
    }
    private fun createPinShortcuts() {
        // 先判斷是否支持添加桌面快捷方式
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            val pinShortcutInfo = ShortcutInfoCompat.Builder(this, locationShortcutId)
                .setShortLabel(getString(R.string.location_shortcuts_short_label))
                .setLongLabel(getString(R.string.location_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
            val pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, pinShortcutInfo)
            val successCallback = PendingIntent.getBroadcast(this, 0, pinnedShortcutCallbackIntent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0)
            ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo, successCallback.intentSender)
        }
    }
}
// 支持用戶創(chuàng)建桌面快捷方式
class CreateCameraShortcutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: LayoutCreateShortcutsActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_create_shortcuts_activity)
        binding.tvTips.text = "Do you want to add the Camera Launcher icon to your home screen?"
        binding.btnAddShortcut.setOnClickListener {
            createPinShortcuts()
        }
        binding.btnReject.setOnClickListener {
            finish()
        }
    }
    private fun createPinShortcuts() {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "camera")
                .setShortLabel(getString(R.string.camera_shortcuts_short_label))
                .setLongLabel(getString(R.string.camera_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_camera))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, CameraActivity::class.java.name)
                })
                .build()
            val pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, pinShortcutInfo)
            setResult(RESULT_OK, pinnedShortcutCallbackIntent)
            finish()
        }
    }
}
// 支持用戶創(chuàng)建桌面快捷方式
class CreateLocationShortcutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: LayoutCreateShortcutsActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_create_shortcuts_activity)
        binding.tvTips.text = "Do you want to add the Location Launcher icon to your home screen?"
        binding.btnAddShortcut.setOnClickListener {
            createPinShortcuts()
        }
        binding.btnReject.setOnClickListener {
            finish()
        }
    }
    private fun createPinShortcuts() {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "location")
                .setShortLabel(getString(R.string.location_shortcuts_short_label))
                .setLongLabel(getString(R.string.location_shortcuts_long_label))
                .setDisabledMessage(getString(R.string.shortcuts_disable_message))
                .setIcon(IconCompat.createWithResource(this, R.drawable.icon_location))
                .setIntent(Intent(Intent.ACTION_VIEW).apply {
                    component = ComponentName(packageName, GpsSignalActivity::class.java.name)
                })
                .build()
            val pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, pinShortcutInfo)
            setResult(RESULT_OK, pinnedShortcutCallbackIntent)
            finish()
        }
    }
}

ExampleDemo github

ExampleDemo gitee

以上就是Android 快捷方式實現(xiàn)實例詳解的詳細內(nèi)容,更多關(guān)于Android 桌面快捷方式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Flutter中嵌入Android 原生TextView實例教程

    Flutter中嵌入Android 原生TextView實例教程

    這篇文章主要給大家介紹了關(guān)于Flutter中嵌入Android 原生TextView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Android編程之內(nèi)存溢出解決方案(OOM)實例總結(jié)

    Android編程之內(nèi)存溢出解決方案(OOM)實例總結(jié)

    這篇文章主要介紹了Android編程之內(nèi)存溢出解決方案(OOM),結(jié)合實例實例總結(jié)分析了Android編程過程中常見的內(nèi)存溢出情況與對應的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲

    Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲

    這篇文章主要介紹了Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Android-自定義控件之ListView下拉刷新的實現(xiàn)

    Android-自定義控件之ListView下拉刷新的實現(xiàn)

    本篇文章主要介紹了Android-自定義控件之ListView下拉刷新示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android  MeasureSpec的理解和源碼的解析

    Android MeasureSpec的理解和源碼的解析

    這篇文章主要介紹了Android MeasureSpec的理解和源碼的解析的相關(guān)資料,希望通過本文大家可以掌握理解這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android實現(xiàn)簡易登陸注冊邏輯的實例代碼

    Android實現(xiàn)簡易登陸注冊邏輯的實例代碼

    在android的應用中越來越多的包含了網(wǎng)絡互動功能,這就帶來了注冊,登陸賬號功能,這篇文章主要給大家介紹了關(guān)于Android實現(xiàn)簡易登陸注冊邏輯的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • Android應用更新之自動檢測版本及自動升級

    Android應用更新之自動檢測版本及自動升級

    這篇文章主要為大家詳細介紹了Android應用更新之自動檢測版本及自動升級,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Android實現(xiàn)京東App分類頁面效果

    Android實現(xiàn)京東App分類頁面效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)京東App分類頁面效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Android?Retrofit使用詳細教程

    Android?Retrofit使用詳細教程

    Retrofit是Android用來接口請求的網(wǎng)絡框架,內(nèi)部是基于OkHttp實現(xiàn)的,retrofit負責接口請求的封裝,retrofit可以直接將接口數(shù)據(jù)解析為Bean類、List集合等,直接簡化了中間繁瑣的數(shù)據(jù)解析過程,這篇文章主要介紹了Android?Retrofit使用詳情,需要的朋友可以參考下
    2024-03-03
  • Android使用ViewPager實現(xiàn)類似laucher左右拖動效果

    Android使用ViewPager實現(xiàn)類似laucher左右拖動效果

    這篇文章主要為大家詳細介紹了Android使用ViewPager實現(xiàn)類似laucher左右拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論