Android创建桌面快捷方式几种方法
fmms
13年前
Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。 <p> 谈谈在桌面上直接生成。个人觉得这个比较爽快,既然都是快捷方式了干嘛还要再隐藏一层呢?当然喜欢桌面干净的就比较喜欢第二个了。</p> <p> 第一个是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。</p> <p> 在网上找到关于这方面的注册信息。</p> <div style="border-bottom:windowtext 0.5pt solid;border-left:windowtext 0.5pt solid;padding-bottom:4px;padding-left:5.4pt;width:97%;padding-right:5.4pt;background:#e6e6e6;word-break:break-all;border-top:windowtext 0.5pt solid;border-right:windowtext 0.5pt solid;padding-top:4px;" id="div_code"> <div> <!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware) <br /> http://www.CodeHighlighter.com/ <br /> <br /> --> <span style="color:#000000;"><</span> <span style="color:#000000;">!</span> <span style="color:#000000;">--</span> <span style="color:#000000;">设置wallpapaer的activity</span> <span style="color:#000000;">--></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">!</span> <span style="color:#000000;">--</span> <span style="color:#000000;"> Intent received used</span> <span style="color:#0000ff;">to</span> <span style="color:#000000;"> install shortcuts from other applications</span> <span style="color:#000000;">--></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">receiver<br /> android:name</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">com.android.launcher2.InstallShortcutReceiver</span> <span style="color:#800000;">"</span> <span style="color:#000000;"><br /> android:permission</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">com.android.launcher.permission.INSTALL_SHORTCUT</span> <span style="color:#800000;">"</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">intent</span> <span style="color:#000000;">-</span> <span style="color:#0000ff;">filter</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">action android:name</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">com.android.launcher.action.INSTALL_SHORTCUT</span> <span style="color:#800000;">"</span> <span style="color:#000000;"> </span> <span style="color:#000000;">/></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"></</span> <span style="color:#000000;">intent</span> <span style="color:#000000;">-</span> <span style="color:#0000ff;">filter</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"></</span> <span style="color:#000000;">receiver</span> <span style="color:#000000;">></span> </div> </div> <p> 可以看出,要在桌面上创建快捷方式就需要权限了:</p> <p> android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。</p> <p> 所以在我们的manifest.xml文件中,我们需要加入下面这段话:</p> <div style="border-bottom:windowtext 0.5pt solid;border-left:windowtext 0.5pt solid;padding-bottom:4px;padding-left:5.4pt;width:97%;padding-right:5.4pt;background:#e6e6e6;word-break:break-all;border-top:windowtext 0.5pt solid;border-right:windowtext 0.5pt solid;padding-top:4px;" id="div_code"> <div> <!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware) <br /> http://www.CodeHighlighter.com/ <br /> <br /> --> <span style="color:#000000;"><</span> <span style="color:#000000;">uses</span> <span style="color:#000000;">-</span> <span style="color:#000000;">permission android:name</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">com.android.launcher.permission.INSTALL_SHORTCUT</span> <span style="color:#800000;">"</span> <span style="color:#000000;">/></span> </div> </div> <p> 下面就是代码层的实现:</p> <p> 假如我在一个activity中创建一个创建快捷方式的方法:createShortCut();</p> <div style="border-bottom:windowtext 0.5pt solid;border-left:windowtext 0.5pt solid;padding-bottom:4px;padding-left:5.4pt;width:97%;padding-right:5.4pt;background:#e6e6e6;word-break:break-all;border-top:windowtext 0.5pt solid;border-right:windowtext 0.5pt solid;padding-top:4px;" id="div_code"> <div> <!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware) <br /> http://www.CodeHighlighter.com/ <br /> <br /> --> <span style="color:#0000ff;">public</span> <span style="color:#000000;"> void createShortCut(){<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">创建快捷方式的Intent<br /> Intent shortcutintent </span> <span style="color:#000000;">=</span> <span style="color:#000000;"> </span> <span style="color:#0000ff;">new</span> <span style="color:#000000;"> Intent(</span> <span style="color:#800000;">"</span> <span style="color:#800000;">com.android.launcher.action.INSTALL_SHORTCUT</span> <span style="color:#800000;">"</span> <span style="color:#000000;">);<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">不允许重复创建<br /> shortcutintent.putExtra(</span> <span style="color:#800000;">"</span> <span style="color:#800000;">duplicate</span> <span style="color:#800000;">"</span> <span style="color:#000000;">,</span> <span style="color:#0000ff;">false</span> <span style="color:#000000;">);<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">需要现实的名称<br /> shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">快捷图片<br /> Parcelable icon </span> <span style="color:#000000;">=</span> <span style="color:#000000;"> Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);<br /> shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">点击快捷图片,运行的程序主入口<br /> shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, </span> <span style="color:#0000ff;">new</span> <span style="color:#000000;"> Intent(getApplicationContext() , EnterActivity.class));<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">发送广播。OK<br /> sendBroadcast(shortcutintent);<br /> }<br /> </span> </div> </div> <p> </p> <p> </p> <p><strong>二、长按桌面弹出的桌面快捷方式创建</strong></p> <p> 第一页谈过直接在桌面生成快捷方式,现在说说如何在添加到一个SHORTCUTS列表中,就是你长按桌面弹出来的那个东东。</p> <p> 首先在注册activity时,需要添加一个action为android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:</p> <p> </p> <div style="border-bottom:windowtext 0.5pt solid;border-left:windowtext 0.5pt solid;padding-bottom:4px;padding-left:5.4pt;width:97%;padding-right:5.4pt;background:#e6e6e6;word-break:break-all;border-top:windowtext 0.5pt solid;border-right:windowtext 0.5pt solid;padding-top:4px;" id="div_code"> <div> <!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware) <br /> http://www.CodeHighlighter.com/ <br /> <br /> --> <span style="color:#000000;"><</span> <span style="color:#000000;">activity android:name</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">ShortCutTest</span> <span style="color:#800000;">"</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">intent</span> <span style="color:#000000;">-</span> <span style="color:#0000ff;">filter</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"><</span> <span style="color:#000000;">action android:name</span> <span style="color:#000000;">=</span> <span style="color:#800000;">"</span> <span style="color:#800000;">android.intent.action.CREATE_SHORTCUT</span> <span style="color:#800000;">"</span> <span style="color:#000000;">/></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"></</span> <span style="color:#000000;">intent</span> <span style="color:#000000;">-</span> <span style="color:#0000ff;">filter</span> <span style="color:#000000;">></span> <span style="color:#000000;"><br /> </span> <span style="color:#000000;"></</span> <span style="color:#000000;">activity</span> <span style="color:#000000;">></span> </div> </div> <p> </p> <p> 接下来就是就是设置快捷方式的图标、名称、事件等属性。这里图表的生成,android里提供了专门的方法来生成。</p> <p> </p> <div style="border-bottom:windowtext 0.5pt solid;border-left:windowtext 0.5pt solid;padding-bottom:4px;padding-left:5.4pt;width:97%;padding-right:5.4pt;background:#e6e6e6;word-break:break-all;border-top:windowtext 0.5pt solid;border-right:windowtext 0.5pt solid;padding-top:4px;" id="div_code"> <div> <!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware) <br /> http://www.CodeHighlighter.com/ <br /> <br /> --> <span style="color:#0000ff;">public</span> <span style="color:#000000;"> class ShortCutTest extends Activity{<br /> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;"> TODO Auto</span> <span style="color:#000000;">-</span> <span style="color:#000000;">generated method stub<br /> super.onCreate(savedInstanceState);<br /> }<br /> <br /> </span> <span style="color:#0000ff;">public</span> <span style="color:#000000;"> void createShortCut(){<br /> Intent addShortCut;<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">判断是否需要添加快捷方式<br /> </span> <span style="color:#0000ff;">if</span> <span style="color:#000000;">(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){<br /> addShortCut </span> <span style="color:#000000;">=</span> <span style="color:#000000;"> </span> <span style="color:#0000ff;">new</span> <span style="color:#000000;"> Intent();<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">快捷方式的名称<br /> addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , </span> <span style="color:#800000;">"</span> <span style="color:#800000;">我的快捷方式</span> <span style="color:#800000;">"</span> <span style="color:#000000;">);<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">显示的图片<br /> Parcelable icon </span> <span style="color:#000000;">=</span> <span style="color:#000000;"> ShortcutIconResource.fromContext(this, R.drawable.icon);<br /> addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">快捷方式激活的activity,需要执行的intent,自己定义<br /> addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, </span> <span style="color:#0000ff;">new</span> <span style="color:#000000;"> Intent());<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">OK,生成<br /> setResult(RESULT_OK, addShortCut);<br /> }</span> <span style="color:#0000ff;">else</span> <span style="color:#000000;">{<br /> </span> <span style="color:#000000;">//</span> <span style="color:#000000;">取消<br /> setResult(RESULT_CANCELED);<br /> }<br /> }<br /> }</span> </div> </div>