2012-04-27 90 views

回答

22

發送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" /> 
+0

謝謝你的解決方案。這對我來說可以。但我有另一個問題。我的應用支持多種語言。所以,當我在設置中更改手機語言時,我的應用程序名稱在啓動器中得到更新。但是應用程序名稱不會更新爲主屏幕中的新語言。我是否需要爲此添加更多內容?感謝您的閱讀.. – Sushil 2014-03-20 00:54:03

+0

由於您無法修改快捷方式,因此這是不可能的。 – volkersfreunde 2014-04-23 12:04:22

+0

優秀。清單中的權限是必需的。 – 2014-09-09 05:55:03

5

第一步,你應該讓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(); 
     } 
    }); 
+0

所以onclick應該有第三節作爲結果? – CreeperHunter 2012-04-27 05:17:24

+0

是的,你只需要這樣做:public void onClick(View v){ \t \t \t \t createShortCut(); \t \t \t} – Laolizi 2012-04-27 06:40:50

2

好吧......我知道這是舊的線程,但我想,以確保工程師訪問此線程有最新信息。

從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."); 

我希望這有助於!

+0

確認,舊廣播「意圖」方式不適用於奧利奧及以上。 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

相關問題