Android 向桌面添加快捷方式

原帖地址:http://www.cnblogs.com/fly_binbin/archive/2010/12/30/1921744.html

方法一:

void setshortCut() {

        Intent addShortcut = new Intent();
        // 設置快捷方式的名字
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式練習");
        // 構建快捷方式中專門的圖標
        Parcelable icon = Intent.ShortcutIconResource.fromContext(
                ShortcutTest.this, R.drawable.icon);
        // 添加快捷方式圖標
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 構建快捷方式執行的Intent
        Intent mailto = new Intent(this, ShortcutTest.class);
        // 添加快捷方式Intent
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto);
        // 正常
        setResult(RESULT_OK, addShortcut);

    }


方法2:

private void addShortcut(){  
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
              
        //快捷方式的名稱  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
//        shortcut.putExtra("duplicate", false); //不允許重複創建  
              
        //指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer  
        //注意: ComponentName的第二個參數必須加上點號(.),否則快捷方式無法啓動相應程序  
       // ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); 
        
      //  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,ShortcutTest.class));  
      
        //快捷方式的圖標  
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  
              
        sendBroadcast(shortcut);  
    }  

   shortcut.putExtra("duplicate", false); //表示的爲不允許重複設置

方法2代碼,直接添加到桌面上,並且也在上圖所示的地方顯示,但是此需要添加下面的權限:

 

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


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