2011-06-13 153 views
51

我想要做的是:Android在主屏幕上創建快捷方式

1)我在一個活動中,有2個按鈕。如果我點擊第一個,我的主屏幕上會創建一個快捷方式。該快捷方式打開了以前已下載的html頁面,所以我希望它使用默認瀏覽器,但我不想使用互聯網因爲我已經擁有該頁面。

2)第二個按鈕來創建一個啓動活性的另一種快捷方式。我想傳遞給活動的一些額外的參數(字符串例如)...........

是那些東西可能嗎?我發現一些鏈接和一些類似的問題,如Android: Is there a programming way to create a web shortcut on home screen

他們似乎是我的問題的答案,但有人告訴我,這個代碼是不會在所有設備上工作,這是不贊成,而我想要做的不是可能.......

這種技術是不推薦。這是一個內部實現,而不是Android SDK的一部分。它不適用於所有主屏幕實現。它可能不適用於所有以前版本的Android。它可能無法在未來的Android版本中使用,因爲Google沒有義務維護內部未記錄的接口。請不要使用此

意味着什麼內部實現?那是值得信賴的代碼或不.....幫我請.....

+6

您應該接受一些答案。人們會更傾向於協助你。 – AedonEtLIRA 2011-06-13 23:24:27

+1

看起來不再允許:「應用及其廣告不得修改或添加瀏覽器設置或書籤,添加主屏幕快捷方式或用戶設備上的圖標作爲第三方的服務或用於廣告目的。」但我不確定,因爲最後一句話。 (來自新政策:http://play.google.com/intl/zh-CN/about/developer-content-policy.html) – Samuel 2014-03-31 07:27:20

回答

80

的示例代碼使用無證接口(許可和意圖)安裝快捷方式。正如「某人」告訴你的,這可能不適用於所有手機,並可能在未來的Android版本中破解。不要這樣做。

正確的方法是傾聽來自家庭screen--快捷方式的要求,在您的清單中,像這樣的意圖過濾器:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label"> 
    <intent-filter> 
    <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

然後在接收意向的活動中,創建一個意圖爲您的快捷方式並將其作爲活動結果返回。

// create shortcut if requested 
ShortcutIconResource icon = 
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 

Intent intent = new Intent(); 

Intent launchIntent = new Intent(this,ActivityToLaunch.class); 

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname()); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 

setResult(RESULT_OK, intent); 
+1

權限和意圖沒有記錄?你的意思是,在文檔中有很多關於意圖的東西,而且你也是有意識的,有什麼不同? amyway,tnx很多,只是讓我們說我的應用程序被稱爲「我的應用程序」,我必須在其清單中添加權限,而在活動中的第二部分顯示2個按鈕來創建快捷方式....但是,您的意思是wirh 「正確的方法是從主屏幕聽取快捷方式請求 - 在清單中使用意向過濾器:」主屏幕如何以及什麼時候要求快捷方式?我的應用程序確實..... – Sgotenks 2011-06-14 00:11:02

+5

@ user280560 - 所有應用程序都將使用權限和意圖;只是一些權限和意圖沒有記錄 - Android將它們用於其內部實現(因爲源代碼是公開的,很容易找到這些),但是你不應該在第三方應用程序中 - 因爲它們是無證可能會在沒有通知的情況下更改。 – antlersoft 2011-06-14 13:41:11

+1

我試過你的代碼,它不會做我想做的事情,而是我發佈的鏈接中的代碼是........這就是你的代碼所做的事情:如果我長按主屏幕並選擇快捷方式,我的MARKET顯示在列表中,如果我選擇它,則會拋出一個意圖(但在桌面上未創建快捷方式)。我想要的是,當我在我的市場內,我點擊一個按鈕,帶有圖標的快捷方式添加到主屏幕..addIntent.setAction(「com.android.launcher。action.Install_SHORTCUT「); this.sendBroadcast(addIntent);這兩行使得它們有什麼差別和邪惡? – Sgotenks 2011-06-14 14:01:21

64

我開發了一種方法來創建android主屏幕上的快捷方式圖標[在我自己的應用程序上測試]。只需調用它。

private void ShortcutIcon(){ 

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.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, "Test"); 
    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); 
} 

不要忘記改變你的活動名稱,圖標資源&許可。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

快樂編碼!!!

編輯:

重複的問題,第一個選項是添加以下行的代碼,否則它會創建新的每次。

addIntent.putExtra("duplicate", false); 

第二個選擇是先卸載應用程序快捷圖標,然後安裝了一遍。如果第一個選項沒有工作。

+2

該解決方案看起來比接受的更簡單。它在我的情況下工作。 – wzbozon 2014-05-27 14:57:57

+21

添加'addIntent.putExtra(「duplicate」,false);否則每次都創建一個新的。 – 2014-07-26 12:05:58

+1

@PratikButani重複不工作在kitkat..will你可以幫我在這 – 2014-09-22 05:13:59

7

我改進了上面的一點解決方案。現在,它會保存首選項,以確定是否已添加快捷方式,如果用戶將其刪除,則不會將其添加到應用程序的新發布中。這也節省了一點時間,因爲添加現有快捷方式的代碼不再運行。

final static public String PREFS_NAME = "PREFS_NAME"; 
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED"; 


// Creates shortcut on Android widget screen 
private void createShortcutIcon(){ 

    // Checking if ShortCut was already added 
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); 
    boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false); 
    if (shortCutWasAlreadyAdded) return; 

    Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.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, "YourAppName"); 
    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(); 
} 
+0

它會在打開應用程序之前添加快捷方式嗎?如何在不從剛剛安裝的應用程序中打開活動的情況下運行此代碼。 – 2014-10-28 09:22:00

+0

使用共享偏好似乎是最好的選擇。其他嘗試**卸載**和**重新安裝**或**添加一個額外的「重複,錯誤」**不適用於大多數設備。 – Dadani 2015-02-17 20:39:40

+0

檢查是否已經存在爲我工作。 – 2016-06-06 12:42:18

2

@Siddiq Abu Bakkar Answer works。但爲了防止每次應用啓動時使用共享首選項創建快捷方式。

final String PREF_FIRST_START = "AppFirstLaunch"; 
SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0); 
    if(settings.getBoolean("AppFirstLaunch", true)){ 

     // record the fact that the app has been started at least once 
     settings.edit().putBoolean("AppFirstLaunch", false).commit(); 
     ShortcutIcon(); 

    } 
1
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); 
+1

感謝R.string.app_name – Frade 2017-01-16 15:23:48

0

由於API level 26,使用com.android.launcher.action.INSTALL_SHORTCUT已棄用。創建快捷方式的新方法是使用ShortcutManager

它提到有3種快捷方式,靜態,動態和固定。 根據您的要求,您可以選擇創建它們。

+0

Android O尚未正式發佈(如果我錯了,請糾正我,「官方」是指官方穩定版)。到目前爲止,Intents的使用還沒有確定 – 2017-08-30 00:34:38

+0

@CodigosTutoriales SDK已經在穩定的版本上發佈,並且手機將很快預裝Oreo版本。始終讓您的應用程序針對並編譯最新的API級別是一種很好的做法。此答案適用於不希望在代碼中看到棄用警告的人。 – noob 2017-08-30 06:05:36

1

在AndroidØ開始,這是創建一個快捷方式:

  if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { 
       final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); 

       ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId) 
         .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher)) 
         .setShortLabel(label) 
         .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) 
         .build(); 
       shortcutManager.requestPinShortcut(pinShortcutInfo, null); 
      } 

它有很大的侷限性,黯然道:

  1. 要求用戶接受添加。
  2. 不能在後臺添加/刪除。
  3. 你應該能夠更新它,但對我來說它不起作用。
  4. 如果目標應用程序被刪除,則不會被刪除。只有創建它的那個。
  5. 根據目標應用程序的資源無法創建圖標,除非它是當前應用程序的圖標。儘管你可以從位圖來完成。

對於Android O之前的版本,您可以使用ShortcutManagerCompat創建新的快捷方式,而沒有任何限制。

相關問題