我想通過按按鈕輕鬆地將我的應用程序添加到主屏幕。所以我在想什麼是我的應用程序底部的一個按鈕,它顯示「添加到主屏幕」,當它被按下時,它將快捷方式添加到主屏幕而不關閉應用程序。 我應該添加什麼代碼要做到這一點?爲Android應用程序添加快捷方式到主屏幕在按鈕上單擊
回答
發送INSTALL_SHORTCUT廣播與作爲額外結果的Intent(在這種情況下,結果Intent直接打開一些活動)。
//where this is a context (e.g. your current activity)
final Intent shortcutIntent = new Intent(this, SomeActivity.class);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
您還需要這個權限在你的清單:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
第一步,你應該讓luncher可以接收廣播:
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
</intent-filter>
</receiver>
接下來,在清單中添加權限.xml
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
最後,創建一個函數並調用它,當你點擊按鈕:
public void createShortCut(){
// a Intent to create a shortCut
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//repeat to create is forbidden
shortcutintent.putExtra("duplicate", false);
//set the name of shortCut
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//set the application to lunch when you click the icon
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
//sendBroadcast,done
sendBroadcast(shortcutintent);
}
做這樣的:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
createShortCut();
}
});
所以onclick應該有第三節作爲結果? – CreeperHunter 2012-04-27 05:17:24
是的,你只需要這樣做:public void onClick(View v){ \t \t \t \t createShortCut(); \t \t \t} – Laolizi 2012-04-27 06:40:50
好吧......我知道這是舊的線程,但我想,以確保工程師訪問此線程有最新信息。
從Android O開始 - 作爲Background Check Limits(本例中爲隱式接收器)的一部分,com.android.launcher.action.INSTALL_SHORTCUT廣播不再對您的應用程序產生任何影響,因爲它現在是一個私有的,隱式廣播。
每安卓ØActivityManagerService.java:
case "com.android.launcher.action.INSTALL_SHORTCUT":
// As of O, we no longer support this broadcasts, even for pre-O apps.
// Apps should now be using ShortcutManager.pinRequestShortcut().
Log.w(TAG, "Broadcast " + action
+ " no longer supported. It will not be delivered.");
我希望這有助於!
確認,舊廣播「意圖」方式不適用於奧利奧及以上。 devs可以使用[ShortcutManagerCompat](https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat.html),當針對API26和更高版本('requestPinShortcut'方法包含回退到'Intent'在<= API25上方,上面使用'ShortcutManager.pinRequestShortcut()') – snachmsm 2017-11-30 14:07:00
- 1. 如何在主屏幕上長按快捷方式菜單
- 2. Android - 添加到主屏幕的快捷方式,並指定屏幕和位置
- 3. Android PWA主屏幕快捷方式
- 4. 將Web快捷方式添加到iPhone主屏幕?
- 5. 將他的應用程序添加到「添加到主屏幕」/「快捷方式」部分
- 6. 如何添加快捷方式到所有應用程序屏幕
- 7. 主屏幕上的快捷方式在我的應用程序中的動作
- 8. 在Android主屏幕上創建URL快捷方式programmaticlly
- 9. Android測試 - 在主屏幕上創建快捷方式
- 10. 在主屏幕上的android的網站的快捷方式
- 11. Android在主屏幕上創建快捷方式
- 12. iOS:應用程序主頁按鈕快捷方式
- 13. 主屏幕快捷方式不起作用。未找到應用程序
- 14. 添加快捷方式到我的程序時,右鍵單擊
- 15. 添加快捷方式到應用程序的菜單
- 16. 如何從主屏幕上自動卸載應用程序快捷方式?
- 17. 添加主屏幕快捷方式時顯示的消息。
- 18. Android:主屏幕快捷方式與可序列化的額外
- 19. 如何使用Codename One在手機的主屏幕中爲按鈕添加快捷方式?
- 20. iPhone app像「添加到主屏幕」按鈕的按鈕樣式
- 21. 以編程方式在Android中刪除所有主屏幕快捷方式
- 22. 如何在android中以編程方式添加應用程序快捷方式
- 23. 在主屏幕上創建特定活動的快捷方式
- 24. 在主屏幕上創建快捷方式
- 25. Ionic 2如何在主屏幕上創建快捷方式
- 26. 如何在android主屏幕上添加一個按鈕?
- 27. 將漸進式Web應用程序添加到主屏幕
- 28. Android主屏幕快捷方式權限錯誤
- 29. 如何以編程方式在Android啓動器的主屏幕上爲特定頁面編寫快捷方式?
- 30. 在屏幕上添加ListView並用按鈕單擊添加元素
謝謝你的解決方案。這對我來說可以。但我有另一個問題。我的應用支持多種語言。所以,當我在設置中更改手機語言時,我的應用程序名稱在啓動器中得到更新。但是應用程序名稱不會更新爲主屏幕中的新語言。我是否需要爲此添加更多內容?感謝您的閱讀.. – Sushil 2014-03-20 00:54:03
由於您無法修改快捷方式,因此這是不可能的。 – volkersfreunde 2014-04-23 12:04:22
優秀。清單中的權限是必需的。 – 2014-09-09 05:55:03