Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。
谈谈在桌面上直接生成。个人觉得这个比较爽快,既然都是快捷方式了干嘛还要再隐藏一层呢?当然喜欢桌面干净的就比较喜欢第二个了。
第一个是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。
在网上找到关于这方面的注册信息。
<style type="text/css">
<!--
#div_code img
{border:0px}
-->
</style>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><!--设置wallpapaer的activity
-->
<!-- Intent received used
to install shortcuts from other applications
-->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT"
/>
</intent-filter>
</receiver>
可以看出,要在桌面上创建快捷方式就需要权限了:
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。
所以在我们的manifest.xml文件中,我们需要加入下面这段话:
<style type="text/css">
<!--
#div_code img
{border:0px}
-->
</style>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
下面就是代码层的实现:
假如我在一个activity中创建一个创建快捷方式的方法:createShortCut();
<style type="text/css">
<!--
#div_code img
{border:0px}
-->
</style>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public void createShortCut(){
//创建快捷方式的Intent
Intent shortcutintent =
new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建
shortcutintent.putExtra("duplicate",
false);
//需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
//快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
//发送广播。OK
sendBroadcast(shortcutintent);
}
二、长按桌面弹出的桌面快捷方式创建
第一页谈过直接在桌面生成快捷方式,现在说说如何在添加到一个SHORTCUTS列表中,就是你长按桌面弹出来的那个东东。
首先在注册activity时,需要添加一个action为android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:
<style type="text/css">
<!--
#div_code img
{border:0px}
-->
</style>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><activity android:name="ShortCutTest">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter>
</activity>
接下来就是就是设置快捷方式的图标、名称、事件等属性。这里图表的生成,android里提供了专门的方法来生成。
<style type="text/css">
<!--
#div_code img
{border:0px}
-->
</style>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public class ShortCutTest extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public void createShortCut(){
Intent addShortCut;
//判断是否需要添加快捷方式
if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
addShortCut =
new Intent();
//快捷方式的名称
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "我的快捷方式");
//显示的图片
Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//快捷方式激活的activity,需要执行的intent,自己定义
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent());
//OK,生成
setResult(RESULT_OK, addShortCut);
}else{
//取消
setResult(RESULT_CANCELED);
}
}
}
源码资料下载:
Android 15本经典教材和应用功能150多个实例源码
分享到:
相关推荐
Android学习笔记(一)——创建第一个Android项目 Android学习笔记(二)android studio基本控件及布局(实现图片查看器) Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) ...
Android学习笔记(十二)——使用意图传递数据的几种方式 完整代码
黑马程序员——Android移动开发源码
Android底层开发技术实战详解——内核、移植和驱动
基于Android的绿色外语社交APP——“Easy Talk”的设计与开发.pdf
Android应用开发完全自学手册——核心技术、传感器、2D/3D、多媒体与典型案例 光盘 吴亚锋,于复兴 编著 人民邮电出版社 2012-3 本书共分21章,内容讲解上深入浅出,实例紧贴开发需求。在技术理论的讲解方面,...
安卓Android源码——安卓Android学习——数据存储.rar
NULL 博文链接:https://trylovecatch.iteye.com/blog/1169537
安卓Android源码——安卓Android Launcher 桌面分页滑动代码.rar
移动操作系统原理与实践——基于Java语言的Android应用开发 目录 基础篇 第1章移动操作系统概论 1.1操作系统的原理与概念 1.1.1隐藏硬件细节 1.1.2资源管理 1.1.3操作系统的历史 1.2操作系统的分类 1.2.1...
这是一个完整的eclipse项目,直接打开就可以查看相关代码
Google Android SDK开发范例大全(台湾天珑书局09年超级畅销书)——示例代码,第三章
安卓Android源码——安卓Android学习——数据存储.zip
Android系统级深入开发——移植与调试 pdf
创建桌面快捷方式源代码(android),作为app开发参考用。
Android学习笔记(一)——创建第一个Android项目 Android学习笔记(二)android studio基本控件及布局(实现图片查看器) Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) ...
Android系统级深入开发——移植与调试.pdf 文件太大,上传网盘,提供下载地址 如果地址失效,请给我邮件,邮件标题注明: csdn Android系统级深入开发——移植与调试.pdf 失效,我将会提供新链接。否则勿扰。
Android学习笔记(十)——使用意图链接活动 完整代码