android create desktop icon(创建桌面图标)

本文介绍了如何通过构造特定的Intent并发送广播给Launcher的BroadcastReceiver来创建Android桌面图标。详细讲解了所需权限、Intent的action设置以及快捷图标所需的图标、名称和启动组件Intent的添加方法。还给出了创建拨打固定电话和启动当前程序的桌面应用的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

看到eoe里面的一个分享帖子,创建桌面图标,感觉分析很透彻,整理过后分享.

Launcher为了让其他应用程序能够定制自己的快捷图标,就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用sendBroadcast方法就可以创建一个快捷图标了。那么,要构造怎样一个Intent才会被Launcher的BroadcastReceiver接收呢?我们还是先来看看这个BroadcastReceiver的注册信息吧。

       下面是Launcher的AndroidManifest.xml文件中Install-ShortcutReceiver的注册信息。
java代码:

  1. <!– Intent received used to install shortcuts from other applications –>
  2. <receiver
  3. android:name=”.InstallShortcutReceiver”
  4. android:permission= “com.android.launcher.permission.INSTALL_SHORTCUT”>
  5. <intent-filter>
  6. <action android:name=”com.android.launcher.action.INSTALL_SHORTCUT” />
  7. </intent-filter>
  8. </receiver>

  如何向这个 BroadcastReceiver 发送广播,设置如下:

        1.首先应用程序必须要有com.android.launcher.permission.INSTALL_SHORTCUT权限;
        2.然后广播出去的Intent的action设置com.android.launcher.action.INSTALL_SHORTCUT;
        3.这样广播就可以发送给Launcher的InstallShortcutReceiver了;

        而快捷图标的信息则是以附加信息的形式存储在广播出去的Intent对象中的,包括有图标、显示的名称以及用来启动目标组件的Intent这三种信息。我们可以通过putExtra的重载方法,通过指定相应的键值,将这些信息放到附加信息的Bundle对象中。

创建一个拨打固定电话的桌面应用:

  1. private final String ACTION_ADD_SHORTCUT =
  2. “com.android.launcher.action.INSTALL_SHORTCUT”;
  3. Intent addShortcut =new Intent(ACTION_ADD_SHORTCUT);
  4. String numToDial = null;
  5. Parcelable icon = null;

  6. numToDial = “110″;
  7. icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

  8. //numToDial = “119″;
  9. //icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);

  10. //图标
  11. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);

  12. //名称
  13. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);

  14. //启动目标组件的Intent
  15. Intent directCall;
  16. directCall.setData(Uri.parse(“tel://”+numToDial));
  17. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,directCall);
  18. sendBroadcast(addShortcut);

启动当前程序的应用:

Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    Parcelable icon=Intent.ShortcutIconResource.fromContext(Main.this, R.drawable.icon_1_n); //获取快捷键的图标  
    Intent myIntent=new Intent(); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");//快捷方式的标题 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//快捷方式的图标      
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的动作          
    sendBroadcast(addIntent);//发送广播

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值