[英]Remove the app icon from the pinned shortcut android
8 android后,我的應用程序圖標出現在快捷方式上。
我知道這是專門添加來通知用戶哪個應用程序創建了快捷方式。
這是當前用於創建圖標的代碼。
ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
BitmapDrawable bd = (BitmapDrawable) ic;
if (shortcutManager.isRequestPinShortcutSupported()) {
Intent shortcutInfoIntent = new Intent(context, MainActivity.class);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
shortcutInfoIntent.putExtra("pckg", pck);
ShortcutInfo info = new ShortcutInfo.Builder(context, "iconId")
.setIcon(Icon.createWithBitmap(bd.getBitmap()))
.setShortLabel(label)
.setIntent(shortcutInfoIntent)
.build();
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
我知道這是一個相當流行的問題,但沒有人提供正常的解決方案。 但我需要以某種方式把它拿走
我確信這是可能的,因為它是在x icon changer中實現的,其中創建的圖標沒有這些徽章。
請寫下你所有的想法如何解決它
我確信這是可能的,因為它是在 x 圖標轉換器中實現的,其中創建的圖標沒有這些徽章。
他們為此使用應用程序小部件。 請參閱您鏈接到的 Play 商店列表中的“關於水印”。
請寫下你所有的想法如何解決它
編寫一個應用小部件。
顯然,使用推薦的shortcutManager.createShortcutResultIntent()
會導致在它創建的快捷方式的一角顯示應用程序圖標,但已棄用的Intent.EXTRA_SHORTCUT_INTENT
方式仍然有效,如果您使用這種方式,創建的快捷方式中不會有應用程序圖標並通過小部件屏幕創建您的快捷方式!
所以第一步是讓您的快捷方式顯示在小部件屏幕上。 為此,請確保<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
在您的 AndroidManifest.xml 文件中,並創建一個過濾<action android:name="android.intent.action.CREATE_SHORTCUT"/>
。 清單文件將如下所示。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<application
...>
...
<activity
android:name=".shortcuts.YourShortcut"
android:label="@string/shortcut_name"
android:icon="@mipmap/ic_launcher"
android:exported="true"
...>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
...
在您的活動中,您需要創建快捷方式。 通常我們使用帶有RESULT_OK
作為結果代碼的setResult()
和shortcutManager.createShortcutResultIntent()
為第二個參數創建意圖,這將導致應用程序圖標顯示在快捷方式的角落。 為了避免使用快捷方式圖標,我們可以使用已棄用的Intent.EXTRA_SHORTCUT_INTENT
類型手動創建意圖。 就像是:
fun onOpen() {
// things to do when user click the shortcut...
}
private fun setupShortcut() {
val intent = Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, Intent(this, this::class.java)) // we use this::class.java here, then clicking the shortcut will trigger onOpened()
.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortLabel)
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, iconResource));
setResult(
RESULT_OK,
intent
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.action == Intent.ACTION_CREATE_SHORTCUT) {
setupShortcut()
} else {
onOpened()
}
finish()
}
完成這些操作后,您可以看到快捷方式出現在“小部件”屏幕上。 當用戶將圖標拖到啟動器上時,角落里也不會顯示應用程序圖標。
請注意,使用不同的啟動器時,實際行為可能仍會有所不同(在某些啟動器下可能無法正常工作)。 以上代碼在 Android 11 和 Android 12 下使用 Pixel Launcher 和 Microsoft Launcher 進行了測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.