Android之生成桌面快捷方式(一)

生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。

谈谈在桌面上直接生成。个人觉得这个比较爽快,既然都是快捷方式了干嘛还要再隐藏一层呢?当然喜欢桌面干净的就比较喜欢第二个了。

第一个是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。

在网上找到关于这方面的注册信息。

InstallShortcutReceiver的注册信息:

复制代码
 <!--设置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文件中,我们需要加入下面这段话:

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


下面就是代码层的实现:

假如我在一个activity中创建一个创建快捷方式的方法:createShortCut();

复制代码
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);
}
复制代码




posted @   HuaDeFei  阅读(13655)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示