Android 爲程序添加桌面快捷方式

        經過我們一番辛勤勞動過後,看到自己的程序在設備上面跑的時候,或多或少都有點成功感!但程序安裝之後,一般來說,程序圖標只顯示在程序菜單之中,那在我的桌面上呢?有兩種方式可以把程序的圖標顯示在桌面上:

       第一:就是在程序菜單中,長按需要顯示在桌面上的程序圖標,當背景切換到桌面的時候,鬆開手指,程序的圖標自然會顯示在桌面上;

但是作爲手機或者平板的使用者,追求的是方便,好用。我們當然需要滿足他們的要求啦!接下來,說說在程序源碼中添加桌面快捷方式,也是這篇文章的重點:

       第二:源碼中生成桌面快捷方式

              第1步,在AndroidManifest.xml文件中添加相應的權限:

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


              第2步,在適合的地方添加生成快捷方式代碼:

    	              Intent shortcutIntent = new Intent(  
    	                            "com.android.launcher.action.INSTALL_SHORTCUT");  
    	              shortcutIntent.putExtra("duplicate",false);   
    	              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
    	                            getString(R.string.app_name));  
    	   
        	              Parcelable icon = Intent.ShortcutIconResource.fromContext(  
    	                            getApplicationContext(),R.drawable.ic_launcher);  
    	   
    	              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);  
    	   
    	              Intent intent = new Intent(getApplicationContext(),  
    	                            ShortCutActivity.class);  
    	             intent.setAction("android.intent.action.MAIN");  
    	             intent.addCategory("android.intent.category.LAUNCHER");    
    	              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);   
    	              sendBroadcast(shortcutIntent);  

       編譯,運行!看到快捷方式了吧?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章