在桌面上添加一个组件的快捷方式很简单,只要长按桌面 或者点击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();
}