假設,我有一些可以幫助用戶安裝其他應用的Android應用。有什麼方法可以在主屏幕上創建這個應用程序的快捷方式嗎?我也可以指定這些快捷方式的位置嗎?如何以編程方式在Android中創建另一個應用的快捷方式?
回答
試試這個:
public void createShortCut{
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
sendBroadcast(shortcutintent);
}
thanx代碼,但是有沒有辦法在創建一個新的之前檢查是否已經存在類似的捷徑? –
嘗試用shortcutintent.putExtra(「duplicate」,false); – Piero
如何使用此代碼創建URL快捷方式? –
一種方法是創建一個小部件。但是,用戶必須再次將小部件放置在主屏幕上才能使用。
A bit improved version: we are checking if the shortcut has been already created
and must not be created if user remove it from the screen
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
@Override
public void onStart(){
super.onStart();
// Checking if ShortCut was already added
sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
if (!shortCutWasAlreadyAdded) createShortcutIcon();
}// end onStart
// Creates shortcut on Android widget screen
private void createShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), Splash.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE
, Intent.ShortcutIconResource.fromContext(getApplicationContext()
, R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
// Remembering that ShortCut was already added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
editor.commit();
objPublicDelegate.showToast(getString(R.string.app_name)+ " shortcut created on screen.");
}// end createShortcutIcon
謝謝。 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);如果沒有這兩行代碼,有時應用程序崩潰,說「TransactionTooLargeException」。爲了避免這種例外,它必須添加這些行。 –
- 1. 如何以編程方式使用Win32創建快捷方式
- 2. 如何在android中以編程方式添加應用程序快捷方式
- 3. 如何在android中以編程方式更改應用程序圖標而不創建快捷方式?
- 4. 創建應用程序快捷方式
- 5. 以編程方式創建組合桌面快捷鍵「快捷方式」
- 6. 如何從其他Android應用程序創建快捷方式
- 7. 以編程方式安裝應用程序快捷方式
- 8. 如何以編程方式創建打印機的快捷方式?
- 9. 以編程方式在python中創建鍵盤快捷鍵
- 10. 以編程方式在Revit中創建鍵盤快捷鍵
- 11. 以編程方式在用戶桌面上創建Web快捷方式
- 12. 在Android上創建快捷方式
- 13. 如何在Android中以編程方式添加快捷方式到主屏幕
- 14. 如何重新創建webstart應用程序的快捷方式?
- 15. 如何創建Java桌面應用程序的快捷方式?
- 16. 如何創建FOLDER的快捷方式?
- 17. 創建批處理和快捷方式的快捷方式
- 18. C#創建快捷方式
- 19. 在Cocoa/XCode中以編程方式添加快捷方式
- 20. 在metro風格的應用程序中創建快捷方式
- 21. Android - 從adb創建快捷方式
- 22. Android應用中的快捷方式
- 23. 如何創建關機快捷方式?
- 24. 在目錄中創建應用程序快捷方式
- 25. 如何在android中關閉應用程序快捷方式?
- 26. 如何在KDE中以編程方式創建鍵盤快捷鍵?
- 27. flex:創建另一個文件/文件夾的快捷方式
- 28. 在drupal 7.17中創建快捷方式
- 29. 在winforms中創建快捷方式VS2017
- 30. 在HomeScreen中創建快捷方式
我不明白;你是什麼意思快捷鍵?所以你想爲已安裝的應用程序創建應用程序圖標/小部件? – LotusUNSW
@LotusUNSW是的,我想爲已安裝的應用程序創建應用程序圖標 –