您的位置:首页 > 移动开发 > Android开发

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);//发送广播
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: