创建桌面快捷方式

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

添加桌面快捷方式,非常简单,只需三步:

1、创建一个添加快捷方式的Intent,该Intent的Action为com.android.launcher.action.INSTALL_SHORTCUT。

2、通过为该Intent添加Extra属性来设置快捷方式的标题、图标及快捷方式对应启动的程序。

3、调用sendBroadcast()方法发送广播即可添加快捷方式。

下面用一个简单示例来演示,在该应用程序中,只给出了添加桌面快捷方式的内容,程序的具体应用无须给出,第一次安装该程序后,会在桌面创建快捷方式,以后不会再创建快捷方式。代码如下:

Activity:

package com.home.activity;

import com.example.testshortcut.R;

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.widget.Toast;

public class TestShortcutActivity extends Activity {
	private SharedPreferences sp;
	private Editor editor;
	private int count = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取SharedPreferences对象
		sp = this.getSharedPreferences("testshortcut", MODE_PRIVATE);
		// 得到Editor对象
		editor = sp.edit();
		// 读取SharedPreferences对象中键为count的值
		int readCount = sp.getInt("count", 0);
		if (readCount > 0) {
			Toast.makeText(this, "快捷方式已存在,不必再创建", Toast.LENGTH_LONG).show();
			return;
		}
		// 创建添加快捷方式的Intent
		Intent addIntent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 快捷方式的标题
		String title = "我的应用";
		// 加载快捷方式图标
		Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
				R.drawable.ic_launcher);
		// 创建点击快捷方式后再次启动的程序,这里启动自己
		Intent myIntent = new Intent(this, TestShortcutActivity.class);
		// 设置快捷方式的标题
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
		// 设置快捷方式的图标
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 设置快捷方式对应的Intent
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
		// 发送广播添加快捷方式
		sendBroadcast(addIntent);
		// 把计数写入文件
		editor.putInt("count", count);
		// 提交修改
		editor.commit();
	}

}

权限:

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

Activity配置:

 <activity
            android:name="com.home.activity.TestShortcutActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- 定义添加到桌面Launcher中 -->
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>




 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值