【android开发】桌面快捷方式

本文介绍如何在Android设备上为应用程序创建桌面快捷方式。通过在AndroidManifest.xml中添加特定的IntentFilter,并在应用活动中配置快捷方式的名称、图标及执行行为,可以实现自定义快捷方式的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在桌面上添加一个组件的快捷方式很简单,只要长按桌面 或者点击menu键,就可弹出添加桌面组件的选项  shortcuts为添加快捷方式。下面通过代码将应用程序添加到shortcuts列表中

1.首先在要设置为快捷方式的应用程序中,添加一个IntentFilter

在AndroidManifest.xml中

 

<activity android:name=".AlarmActivity"
 android:label="@string/app_name">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter> </activity>

 2.设置快捷方式的名字,图标,事件等属性,在组件的onCreate方法中设置

public void onCreate(Bundle b) {
       	super.onCreate(b);
	// 要添加的快捷方式Intent
	Intent addShortcut;
//	判断是否要添加快捷方式
	if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {

		addShortcut = new Intent();
//设置名字
		addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"短信发送器");
//快捷方式使用图片
		Parcelable icon = Intent.ShortcutResource.fromContext(this,R.drawable.icon);
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
//快捷方式要执行的Intent
Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:xxx@xx.com"));
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,mailto);
//ok
setResult(RESULT_OK,addShortcut);
} else {
	setResult(RESULT_CANCEL);
}
finish();
}
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值