android create desktop icon(创建桌面图标)
2011-08-24 14:35
721 查看
看到eoe里面的一个分享帖子,创建桌面图标,感觉分析很透彻,整理过后分享.
Launcher为了让其他应用程序能够定制自己的快捷图标,就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用sendBroadcast方法就可以创建一个快捷图标了。那么,要构造怎样一个Intent才会被Launcher的BroadcastReceiver接收呢?我们还是先来看看这个BroadcastReceiver的注册信息吧。
下面是Launcher的AndroidManifest.xml文件中Install-ShortcutReceiver的注册信息。
java代码:
<!– Intent received used to install shortcuts from other applications –>
<receiver
android:name=”.InstallShortcutReceiver”
android:permission= “com.android.launcher.permission.INSTALL_SHORTCUT”>
<intent-filter>
<action android:name=”com.android.launcher.action.INSTALL_SHORTCUT” />
</intent-filter>
</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对象中。
创建一个拨打固定电话的桌面应用:
private final String ACTION_ADD_SHORTCUT =
“com.android.launcher.action.INSTALL_SHORTCUT”;
Intent addShortcut =new Intent(ACTION_ADD_SHORTCUT);
String numToDial = null;
Parcelable icon = null;
numToDial = “110″;
icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);
//numToDial = “119″;
//icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);
//图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
//名称
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);
//启动目标组件的Intent
Intent directCall;
directCall.setData(Uri.parse(“tel://”+numToDial));
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,directCall);
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);//发送广播
Launcher为了让其他应用程序能够定制自己的快捷图标,就注册了一个BroadcastReceiver专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用sendBroadcast方法就可以创建一个快捷图标了。那么,要构造怎样一个Intent才会被Launcher的BroadcastReceiver接收呢?我们还是先来看看这个BroadcastReceiver的注册信息吧。
下面是Launcher的AndroidManifest.xml文件中Install-ShortcutReceiver的注册信息。
java代码:
<!– Intent received used to install shortcuts from other applications –>
<receiver
android:name=”.InstallShortcutReceiver”
android:permission= “com.android.launcher.permission.INSTALL_SHORTCUT”>
<intent-filter>
<action android:name=”com.android.launcher.action.INSTALL_SHORTCUT” />
</intent-filter>
</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对象中。
创建一个拨打固定电话的桌面应用:
private final String ACTION_ADD_SHORTCUT =
“com.android.launcher.action.INSTALL_SHORTCUT”;
Intent addShortcut =new Intent(ACTION_ADD_SHORTCUT);
String numToDial = null;
Parcelable icon = null;
numToDial = “110″;
icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);
//numToDial = “119″;
//icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.huo);
//图标
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
//名称
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);
//启动目标组件的Intent
Intent directCall;
directCall.setData(Uri.parse(“tel://”+numToDial));
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,directCall);
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);//发送广播
相关文章推荐
- Android 如何在Launcher桌面创建或者删除快捷图标
- Android创建桌面快捷图标
- android实现桌面icon未读信息图标(类似与iPhone的badge)
- Android中动态更换App的桌面icon图标和activity-alias的使用
- Android开发 创建桌面图标和widget的示例
- (4.6.25)Android动态更换应用Icon之玩转桌面图标
- Android创建桌面图标
- android应用创建桌面快捷图标
- android 创建桌面图标,luncher图标
- Android 关于创建桌面快捷图标的几点笔记
- linux 创建桌面图标 desktop文件
- Linux下通过.desktop 文件创建桌面程序图标及文件编写方式(Desktop Entry文件概述)
- android创建桌面图标
- Android 创建桌面快捷图标
- Extjs2.0 desktop 动态创建桌面图标和开始菜单
- 创建桌面图标和删除桌面图标 android
- Android 开发之创建桌面快捷图标
- ubuntu14.04 自定义创建桌面图标
- Android开发:应用启动后自动创建桌面快捷方式(三星机常见)
- Android应用第一次安装成功点击“打开”后Home键切出应用后再点击桌面图标返回导致应用重启问题