简介
Android 7.0版本有一个新特性:如果app支持,可以通过长按app图标出现一些快捷操作。一些热门应用举例:
1.动态的添加快捷方式
/**
* 添加快捷方式(长按可自由拖拽)
*/
fun onClickshortcutsAdd(context: Context, openClass: Class<*>, name: String, icon: Int) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
var shortcutManager = context.getSystemService(ShortcutManager::class.java) as ShortcutManager
var intent = Intent(context, openClass)
intent.action = Intent.ACTION_VIEW
var shortcut = ShortcutInfo.Builder(context, name)
.setIcon(Icon.createWithResource(context, icon))
.setShortLabel(name)
.setLongLabel(name)
.setIntent(intent)
.build()
shortcutManager.addDynamicShortcuts(listOf(shortcut))
}
} catch (e: Exception) {
SLogs.e(e)
}
}
/**
* 删除快捷方式
*/
fun onClickshortcutDel(context: Context, deleteId: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
var shortcutManager = context.getSystemService(ShortcutManager::class.java) as ShortcutManager
shortcutManager.removeDynamicShortcuts(listOf(deleteId))
}
}