Android 快捷方式是桌面最基本的组件。它用于直接启动某一应用程序的某个组件。

一般情况下,可以在Launcher的应用程序列表上,通过长按某一个应用程序的图标在左面上创建改该应用程序的快捷方式。另外,还可以通过两种方式在桌面上添加快捷方式:

一:在应用程序中创建一个Intent,然后以Broadcast的形式通知Launcher创建一个快捷方式。

二:为应用程序的组件注册某一个符合特定条件的IntentFilter,然后可以直接在Launcher的桌面添加启动该组件的快捷方式。

下面以实例来讲解:

一:在应用程序中添加快捷方式

我们可以在看Launcher 的Androidmanifest.xml文件中Install-ShortcutReceiver的注册信息。

<receiver

android:name=".InstallShortcutReceiver"

android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"

<Intent-filter>

<action android:name="android.launcher.action.INSTALL_SHORTCUT"/>

</Intent-filter>

</receiver>

可以看出来系统是使用Intent的方式。

也就是说我们使用BroadcastReceiver发送广播,首先应该程序需要有com.android.launcher.permission.INSTALL_SHORTCUT权限,完后以广播出去的Intent的action设置为com.android.launcher.action.INSTALL_SHORTCUT.这样就可以发送给Launcher的InstallShortReceiver了。

下面以一个Demo为例,AndroidManifest.xml中注意添加:

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

jing是一张ICON图标

下面添加一个按钮,在Button的OnClickerListener中添加如下:

String numtoDial="110";

Intent directCall = new Intent(ACTION_CALL);

directCall.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);

directCall.setData(Uri.parse("tel://"+numToDial))

Intent addShortcut=new Intent(ACTION_ADD_SHORT);

Parcelable Icon=Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

addShortcut.putEXTRA(Intent.EXTRA_SHORTCUT_INTENT,directCall);

addShortcut.putEXTRA(Intent.EXTRA_SHORTCUT_ICON_RESOUCE,icon);

sendBroadcast(addShortcut);

这样就完成了。点击就可以看见效果。

二:在Launcher添加应用程序的快捷方式。

当我们在Launcher的桌面空白处长按触摸时,会出现一个对话框,提示选择添加的桌面组件。

这里会出现快捷方式的选项。对话框显示出了可添加的快捷方式的Activity所属的应用程序的图标和名称的列表。当我们想把添加快捷方式的Activity添加到这一列表中时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以了。

<activity android:name=".UrgentCall">

.....

<intent-filter>

<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.CREATE_SHORTCUT"/>

</intent-filter>

.....

</activity>

这样,在Select Shortcut中就有了我们添加的这个Activity的添加。

这个的原理只是在所有的Activity中查询是否有android.intent.action.CREATE_SHORTCUT的 Action而已。这样会启动对于的Activity,我们可以在这个里面直接添加代码,直接添加Intent,然后Broadcast.

Activity {

@Override

public void OnCreate(Bundle saveInstantceState)

{

super.onCreate(saveInstantceState);

Intent addshortcut;

if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT))

{

addShortcut =new Intent();

addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"119");

Parcelable icon=Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);

Intent callFirePolice = new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));

addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,callFilePolice);

setResult(RESULT_OK,addShortcut);

}

else

{

setResult(RESULT_CANCELED);

}

finish();

}

}

注意需要在AndroidManifest.xml文件中添加

<intent-filter>

<action android:name="android.Intent.action.CRATE_SHORTCUT"/>

</intent-filter>

}

本文虽然简单,但是还是希望大家能够实际操作一把。

更多相关文章

  1. android 系统
  2. Android应用开发以及设计思想深度剖析(5)
  3. JUST FOR NOTE ---- ANDROID NDK
  4. 钱景无限 Android应用开发者晒收入
  5. 3G之Android学习第三章节>
  6. [置顶] android 跨进程通信
  7. Android动态创建一个个组件
  8. Android(安卓)Tangram模型:淘宝、天猫都在用的UI框架模型你一定要
  9. android launcher之检测应用程序图标在桌面是否存在

随机推荐

  1. Android 多个Activity选项卡实现
  2. android hook getdeceiveid
  3. Android中创建对话框
  4. Android(安卓)Stduio出现“required plug
  5. android ExpandableListView ExpandableL
  6. pandaboard ES学习之旅——4 Android源代
  7. Android 中文 API (29) ―― CompoundButto
  8. 跟着做 Android NDK学习入门如此简单(二)
  9. android数据存取的四种方式
  10. 在activity中调用Application 出现androi