最近感觉这个添加快捷方式挺有趣的,就查资料自己写了个demo---简单的例子,这个例子就是有两个按钮,点击“将此程序添加到快捷方式”,则手机桌面增加一个快捷方式,同时launcher中也多了一个快捷方式,点击退出,则提示:toast弹提示信息“退出程序”。知识梳理:Android平台上添加快捷方式有两种:一种桌面的快捷方式,一种是launcher的快捷方式。原理:是通过intent封装一些信息,以Broadcast的形式通知launcher创建快捷方式的!一定不要忘记在manifest.xml中注册一下权限:

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

在manifest.xml中加入一个动作过滤的intentFilter,快捷方式的列表中会多个该程序的快捷方式。

有问题或向说点什么的可以留言,欢迎大家批评和指正,转载请标明出处:

下面看一下程序的截图:  

                             程序的开始界面:                                     点击“将此程序添加快捷方式”按钮:

                            

点击退出按钮,桌面多了快捷方式,弹Toast:            点出选择快捷方式后多了程序的快捷方式:

                              

在IntentWidget工程中:

一、在com.cn.daming包中IntentWidgetMainActivity.java中的代码:

[java] view plaincopyprint?
  1. <span><span>
  2. </span></span><span style="font-size:13px;color:#000000;">package com.cn.daming;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.Parcelable;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11. public class IntentWidgetMainActivity extends Activity implements OnClickListener{
  12. private Button mStartWidgetButton;
  13. private Button mExitButton;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. mStartWidgetButton = (Button) findViewById(R.id.my_button_1);
  19. mExitButton = (Button) findViewById(R.id.my_button_2);
  20. mStartWidgetButton.setOnClickListener(this);
  21. mExitButton.setOnClickListener(this);
  22. }
  23. public void onClick(View v)
  24. {
  25. if(v == mStartWidgetButton){
  26. //inint the widgetIntent is declear
  27. Intent addWidgetIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  28. <span style="white-space: pre-wrap;"></span>//whether repeat create is or not
  29. addWdgetIntent.putExtra("duplicate",true);<span style="font-family: monospace;font-size:10px;"><span style="white-space: pre-wrap;"><span><span>
  30. </span></span></span></span>
  31. //set the Widget of the title
  32. String mTitle = getResources().getString(R.string.my_title);
  33. //set the Widget of the icon
  34. Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.widget_image);
  35. Intent mIntent = new Intent(this,IntentWidgetMainActivity.class);
  36. addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mTitle);//set the title
  37. addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//set the icon
  38. addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mIntent);//set the intent
  39. sendBroadcast(addWidgetIntent);
  40. }
  41. else if(v == mExitButton){
  42. finish();
  43. Toast.makeText(IntentWidgetMainActivity.this, R.string.exit, Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46. }</span>

二、在layout目录下的main.xml中的代码:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#00ffffff"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="15dip"
  12. android:layout_marginBottom="15dip"
  13. android:gravity="center"
  14. android:text="@string/hello"
  15. android:textSize="8pt"
  16. />
  17. <Button
  18. android:id="@+id/my_button_1"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginBottom="10dip"
  22. android:textSize="10pt"
  23. android:text="@string/my_button_1"
  24. />
  25. <Button
  26. android:id="@+id/my_button_2"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_marginBottom="10dip"
  30. android:textSize="10pt"
  31. android:text="@string/my_button_2"
  32. />
  33. <TextView
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_marginBottom="15dip"
  37. android:gravity="center"
  38. android:text="@string/blogs"
  39. android:textSize="8pt"
  40. />
  41. </LinearLayout>

三、在values下的string.xml中的代码:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">这是大明添加到Launcher的快捷方式</string>
  4. <string name="app_name">大明快捷方式!</string>
  5. <string name="my_button_1">将此程序添加快捷方式</string>
  6. <string name="my_button_2">退出程序</string>
  7. <string name="my_title">大明程序</string>
  8. <string name="exit">程序正在退出。。。。。。</string>
  9. <string name="blogs">博客地址:\n http://blog.csdn.net/wdaming1986/article/details/6877154</string>
  10. </resources>

四、manifest.xml 中的代码

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn.daming"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="8" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".IntentWidgetMainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. <!-- add the launch of my programmer`s quick launcher-->
  15. <intent-filter>
  16. <action android:name="android.intent.action.CREATE_SHORTCUT"/>
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  21. </manifest>

Android中程序向桌面和Launcher添加快捷方式相关推荐

  1. [转]Android中程序与Service交互的方式——交互方式

    本文转自:http://blog.csdn.net/yihongyuelan/article/details/7216188 上一篇文章:Android中程序与Service交互的方式--综述 简述了 ...

  2. Android中程序与Service交互的方式

    本文将通过三大部分来讲解Android中程序与Service的交互方式,这里说的交互方式指的是如何与Service进行消息的传递,比如:从Service中获取信息,向Service发送信息等等.举个简 ...

  3. Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】

    最近研究ICS4.0的Launcher,发现4.0和2.3有稍微点区别,但是区别不是特别大,所以我就先整理一下Launcher启动的大致流程.Launcher其实是贯彻于手机的整个系统的,时时刻刻都在 ...

  4. Android中动态更换桌面图标Icon(记录)

    问题描述:支付宝.美团.淘宝之类的在某些节日的时候,桌面图标会进行自动进行变化,但是并没有进行APP更新之类的,想做这样的效果,怎么弄? 解决方案: (1)使用activity-alias来定义多个程 ...

  5. Android应用程序创建桌面快捷方式

    2019独角兽企业重金招聘Python工程师标准>>> public static final String READ_SETTINGS_PERMISSION = "com ...

  6. android 编程一个程序实现方法,Android中一个应用实现多个图标的几种方式

    前言 最近因为新需求需要,我的应用将有多个ICON入口..最终选择了 activity-alias,其实实现多图标有好几种方式,下面就给大家总结下,分享出来供大家参考学习: 1. 多Activity ...

  7. android 多个应用,Android中一个应用实现多个图标的几种方式

    前言 最近因为新需求需要,我的应用将有多个ICON入口..最终选择了 activity-alias , 其实实现多图标有好几种方式,下面就给大家总结下,分享出来供大家参考学习: 1. 多Activit ...

  8. linux远程桌面MacOS,如何在Linux或macOS中使用远程桌面连接到Windows 10 | MOS86

    如果您运行其他平台(例如Linux或macOS),但偶尔需要使用Windows 10 PC,则可以使用免费的远程桌面软件. 远程桌面协议是一种从网络上的另一台计算机控制Windows 10计算机的直观 ...

  9. Android中RemoteViews的实现

    田海立@CSDN 2012-8-22 本文结合AppWidget的应用场景,分析Android中RemoteViews的内部具体实现. 从前文<Android中AppWidget的分析与应用:A ...

最新文章

  1. 【Enterprise Manager 12c】如何在EM 12c中配置Exadata Infiniband告警邮件
  2. ubuntu 如何右上角显示键盘
  3. 智能理财在国内国外的发展现状
  4. mysqldatareader获取整行数据给datarow_C# sqladapter 与sqldataReader
  5. 博客园配置windows live writer,实现本地代码高亮
  6. apipost脚本使用一
  7. 被迷惑了,两个不相关的文件也有相同的地方
  8. 企业中MySQL高可用集群架构三部曲之MM+keepalived
  9. 伪原创方法-学习一下
  10. Eclipse 中设置编辑器字体,包括 Java 编辑器,XML 编辑器和 Property 编辑器的字体都可以设置
  11. 牛客寒假集训营 牛牛战队的比赛地
  12. 安装PL-2303驱动
  13. 北京精雕现状_6秒精密加工,日本走下神坛,北京精雕也做了一个!
  14. apm性能监控系统,字节跳动Android三面凉凉,再不刷题就晚了!
  15. 方舟无限琥珀服务器,方舟生存进化无限琥珀版
  16. python动态仪表图_matplotlib仪表动态更新
  17. JAVA基础总结----JAVA面试必备
  18. jquery设置checkbox选中和未选中的方式
  19. 消息队列:生产者/消费者模式
  20. JavaWeb-Servlet生命周期

热门文章

  1. 【Flutter】Flutter 布局组件 ( FractionallySizedBox 组件 | Stack 布局组件 | Positioned 组件 )
  2. 【RecyclerView】 十二、RecyclerView 数据更新 ( 修改单条数据 | 批量修改数据 )
  3. 【Kotlin】扩展属性 ( 扩展变量属性 | 扩展常量属性 | 注意事项 | 本质分析 )
  4. Codeforces 1206
  5. Cocos2d-x3.0 不规则Button
  6. 网卡的7种bond模式
  7. 升级python2至python3解决依赖关系
  8. 前端Yslow的23个优化原则
  9. shell脚本--字符串处理和动态数组
  10. maven 在 mac中的配置