2012-04-12 100 views
0

如何在安裝後自動創建快捷方式?下面的代碼不起作用!如何在安裝後自動創建快捷方式?

private void createShortCut() { 
    Intent intent = new Intent(ACTION_INSTALL_SHORTCUT); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
    intent.putExtra("duplicate", false); 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//icon 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , MainActivity.class)); 

    setResult(RESULT_OK, intent); 
    sendBroadcast(intent); 
} 
+0

你檢查過[這一個](http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen) – 2012-04-12 11:59:00

回答

2

安裝後無法自動創建快捷方式,因爲安裝後代碼沒有自動運行。

+0

第一次啓動後可能嗎? – thecr0w 2012-04-13 08:59:02

+1

然後你對Google Play商店中的應用說什麼,他們自動創建快捷方式 – 2014-03-06 08:39:36

+0

@JayVyas:Play商店應用安裝程序正在這樣做,而不是正在安裝的應用。 – CommonsWare 2014-03-06 10:39:05

0

是的,創建快捷方式非常有可能是您的應用程序或任何第三方應用程序或系統應用程序。請遵循此示例代碼。

try 
     { 
      for (int i = 0; i < packageNameArray.length; i++) 
      { 
       Intent shortcutIntent = Util.makeIntent(MyPreference.getAction(context), MyPreference.getCategory(context), packageNameArray[i], ActivityNameArray[i]); 
       Bitmap shortcutIcon2 = Util.confleteBitmap((UtilityClass.StringToBitMap(IconArray[i])), BitmapFactory.decodeResource(context.getResources(), R.drawable.shortcut_mark)); 
       data.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent); 
       data.putExtra("android.intent.extra.shortcut.ICON", shortcutIcon2); 
       data.putExtra("android.intent.extra.shortcut.NAME", AppNameArray[i]); 
       //uninstalling shortcut to remove duplicacy 
       Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
       intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
       intent.putExtra("duplicate", false); 
       intent.putExtras(data); 
       context.sendBroadcast(intent); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

這裏packagenamearray是您選擇創建您通過快捷所有的應用程序,你應用程式包名在這裏,如果你要創建只是你的應用程序快捷方式 activitynamearray是選擇應用程序的activityname

相關問題