参考:http://blog.csdn.net/pku_android/article/details/7438931

直接上源码:

package com.zhang.android.shortcut;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Parcelable;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ShortCutActivity extends Activity {

private Button btn;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

System.out.println("@@@@@@@@@@create@@@@@@@@@@@@@@@@@@@@@@@@");

initLayout();

}

private void initLayout()

{

btn = (Button)this.findViewById(R.id.btn);

btn.setOnClickListener(new BtnOnClickListener());

}

class BtnOnClickListener implements OnClickListener

{

public void onClick(View v) {

// TODO Auto-generated method stub

Intent addShortCut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

String title = ShortCutActivity.this.getString(R.string.title);//名称

Parcelable icon = Intent.ShortcutIconResource.fromContext(ShortCutActivity.this, R.drawable.icon);//图标

//点击快捷方式后操作Intent,快捷方式建立后,再次启动该程序

Intent intent = new Intent(ShortCutActivity.this, ShortCutActivity.class);

//设置快捷方式的标题

addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);

//设置快捷方式的图标

addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

//设置快捷方式对应的Intent

addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

//发送广播添加快捷方式

sendBroadcast(addShortCut);

}

}

}

package="com.zhang.android.shortcut"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".ShortCutActivity"

android:label="@string/app_name" >

特别注意权限的添加。

说明:每点击一次按钮都会创建一个桌面快捷方式。点击桌面快捷方式会执行该应用程序。

源码下载:http://download.csdn.net/my

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐