2013-07-02 72 views
1

創建一個獨特的快捷鍵安裝應用時行爲有所我用下面的代碼來創建一個快捷方式安裝的應用程序時:不同的Android版本

在AndroidManifest.xml

<!-- for creating a shortcut in the home screen --> 
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

中的onCreate( )主要活動:

// an 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, this.getString(R.string.app_name)); 
    //set icon 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher); 
    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() , MainActivity.class)); 
    //sendBroadcast,done 
    sendBroadcast(shortcutIntent); 

這些代碼在安卓4.0.4,其產生在第一時間快捷方式,併發送舉杯說的快捷方式已經EXI做工精細sts在第一次安裝後。但在Android 4.2.2中,我可以通過單擊後退鍵創建許多重複的快捷方式,然後再次輸入應用程序。

有沒有什麼辦法可以在兩個版本的Android上工作?

感謝提前:)

回答

-1

shortcutIntent.putExtra( 「重複」,FALSE)未在4.2版本的工作。我認爲你必須用其他方法來創造獨特的捷徑。

如果failour則只需卸載快捷方式,安裝again.something,如:

addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
getApplicationContext().sendBroadcast(addIntent);    
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
getApplicationContext().sendBroadcast(addIntent); 

和艙單同時使用UNINSTALL_SHORTCUT和INSTALL_SHORTCUT權限。

而這個粗略的想法很好地適用於每個版本。

相關問題