---
title: "为Android软件创建快捷方式"
author: "小建枫叶"
date: "2012-04-13T11:36:49Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-04-13T11:40:07Z"
id: 1485098
url: "https://smallmaple.iteye.com/blog/1485098"
---

注意：需要添加权限&lt;uses-permission android:name="com.android.launcher.permission.INSTALL\_SHORTCUT" /&gt;

以下是全部代码：

 

```
package shortcut.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class ShorcutDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        makeShortCut();
    }
    
    /**
	 * 创建桌面快捷方式
	 */
	private void makeShortCut() {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
		Editor editor = sp.edit();
		boolean isFirstStart = sp.getBoolean("isFirstStart", true);
		if(!isFirstStart) return;
		
        Parcelable appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); //获取快捷键的图标            
        String appName = getString(R.string.app_name);
        Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
        Intent myIntent=new Intent(this, ShorcutDemoActivity.class);
        
        addIntent.putExtra("duplicate", false); //不允许重复创建
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);//快捷方式的标题           
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appIcon);//快捷方式的图标              
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的动作    
        sendBroadcast(addIntent);//发送广播
        editor.putBoolean("isFirstStart", false);
        editor.commit(); 
        Toast.makeText(this, "成功创建”"+ appName +"“快捷方式", Toast.LENGTH_LONG).show();
	}
}
```