手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android中创建快捷方式代码实例
Android中创建快捷方式代码实例
摘要:1、添加权限(必须)复制代码代码如下:2、添加快捷键复制代码代码如下:publicstaticvoidsetupShortcut(Activ...

1、添加权限(必须)

复制代码 代码如下:

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

2、添加快捷键

复制代码 代码如下:

public static void setupShortcut(Activity activity)

{

Intent shortcutIntent = new Intent(activity, MainActivity.class); //启动首页(launcher Activity)

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");//快捷键名字可以任意,不过最好为app名称

Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

intent.putExtra("duplicate", false);//不允许重复创建

activity.sendBroadcast(intent);//发送广播创建快捷键

}

3、快捷键也可以指向非Launcher activity,只需要在AndroidManifest中对应的Activity 中添加如下配置

复制代码 代码如下:

<intent-filter>

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

<intent-filter>

例如可以将2 中的MainActivity 改为任意其他Activity,同时在AndroidManifest中对应添加上述intent-filter就可以了。

【Android中创建快捷方式代码实例】相关文章:

android实现横屏的代码及思路

Android中ActionBar以及menu的代码设置样式

Android 图片特效处理的方法实例

Android里实现退出主程序的提示代码

Android中的Button自定义点击效果实例代码

android二级listview列表实现代码

android屏幕全屏的实现代码

Android图片处理实例介绍(图)

Android在OnCreate中获取控件的宽度和高度的实现代码

Android 五种布局模式

精品推荐
分类导航