Androidはデスクトップにショートカットを追加します。

2062 ワード

元の住所:http://www.cnblogs.com/fly_ビバーン/archive/2010/12/30/1921744.
方法1:
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);  
    }  
   ショートカット.put Extra(「duplicate」、false);重複設定が許されないことを示します。
方法2コードをデスクトップに直接追加し、上の図の場所にも表示しますが、以下のパーミッションを追加する必要があります。
 
<uses-permission android:name=「comp.android.launcher.permission.INSTALLUSHORTCUT」/>