Android中为你的应用程序添加桌面快捷方式
来源:互联网 发布:手机人肉搜索软件 编辑:程序博客网 时间:2024/04/27 22:11
如需转载引用请注明出处:http://blog.csdn.net/jiahui524
相信大家在使用很多的应用的时候都碰到过这样的一个情景,那就是当我们第一次点击使用这个应用的时候程序会主动的为我们创建一个桌面上的快捷方式,这样的好处是可以让用户更加方便的进入这个应用,省去找的麻烦。有句话曾经说,谁占据了PC桌面客户端谁就是王者,像QQ,360等都是。所以我们在做开发的时候肯定也会有用到这一点,那么今天我就和大家分享实现的这样的一个功能。其实不难,你只需要在你的应用程序启动的第一个Activity里添加这样的一个方法:
- /**
- * 创建快捷方式
- */
- public void createDeskShortCut() {
- Log.i("coder", "------createShortCut--------");
- // 创建快捷方式的Intent
- Intent shortcutIntent = new Intent(
- "com.android.launcher.action.INSTALL_SHORTCUT");
- // 不允许重复创建
- shortcutIntent.putExtra("duplicate", false);
- // 需要现实的名称
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- getString(R.string.app_name));
- // 快捷图片
- Parcelable icon = Intent.ShortcutIconResource.fromContext(
- getApplicationContext(), R.drawable.ic_launcher);
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
- Intent intent = new Intent(getApplicationContext(),
- AndroidLayoutActivity.class);
- // 点击快捷图片,运行的程序主入口
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
- // 发送广播。OK
- sendBroadcast(shortcutIntent);
- }
如果只是添加这些代码,当你卸装你的应用程序的时候你又会发现存在一个问题就是你的应用程序虽然卸载了,可是桌面上的快捷方式并未卸载。呵呵,其实你只要在你要设置对应启动进入的那个Intent加上这么下面的两个属性就是表明与你的应用绑定了。
- // 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
给上完整的代码:
- package com.jiahui.layout;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.util.Log;
- public class AndroidLayoutActivity extendsActivity {
- /**Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view_personal_info);
- SharedPreferencespreferences = getSharedPreferences("first",
- Context.MODE_PRIVATE);
- booleanisFirst = preferences.getBoolean("isfrist", true);
- if(isFirst) {
- createDeskShortCut();
- }
- SharedPreferences.Editoreditor = preferences.edit();
- editor.putBoolean("isfrist",false);
- editor.commit();
- }
- /**
- * 创建快捷方式
- */
- publicvoid createDeskShortCut() {
- Log.i("coder","------createShortCut--------");
- //创建快捷方式的Intent
- IntentshortcutIntent = new Intent(
- "com.android.launcher.action.INSTALL_SHORTCUT");
- //不允许重复创建
- shortcutIntent.putExtra("duplicate",false);
- //需要现实的名称
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- getString(R.string.app_name));
- //快捷图片
- Parcelableicon = Intent.ShortcutIconResource.fromContext(
- getApplicationContext(),R.drawable.ic_launcher);
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
- Intentintent = new Intent(getApplicationContext(),
- AndroidLayoutActivity.class);
- //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- //点击快捷图片,运行的程序主入口
- shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
- //发送广播。OK
- sendBroadcast(shortcutIntent);
- }
- }
哦,亲,千万别忘记在AndroidManifest.xml加上下面的这个权限:
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
最终效果图:
0 0
- Android中为你的应用程序添加桌面快捷方式
- Android中为你的应用程序添加桌面快捷方式
- Android中为你的应用程序添加桌面快捷方式
- Android为你的应用程序添加桌面快捷方式
- 为android应用程序添加桌面快捷方式
- Android 为你的应用程序添加快捷方式【优先级高的快捷方式】
- Android 为你的应用程序添加快捷方式【优先级高的快捷方式】
- 在Android桌面添加应用程序的快捷方式图标
- Android 为应用程序创建桌面快捷方式
- WinCE桌面添加应用程序的快捷方式
- WinCE桌面添加应用程序的快捷方式
- WinCE桌面添加应用程序的快捷方式
- WinCE桌面添加应用程序的快捷方式
- WinCE桌面添加应用程序的快捷方式
- Android 为程序添加桌面快捷方式
- Android 为程序添加桌面快捷方式
- android桌面添加快捷方式
- Android添加桌面快捷方式
- mongodb与关系型数据库相比的优缺点zz
- 彩信之SMIL规范
- debian设置非utc
- ios--设计模式--单例模式
- 获取上海银行间拆放利率(Shibor)
- Android中为你的应用程序添加桌面快捷方式
- Windows下配置apache的一个小陷阱
- Android项目打第三方jar包
- Ubuntu下连接Android手机真机调试不识别问题
- MySQL 5.1升级到MySQL 5.5的步骤
- 使用dlopen和dlsym来使用C++中的类
- 你不必太完美
- Linux中的Printk与dmesg功能--LOG(2)
- flex鼠标滚轮事件(鼠标滚动后,数字变换)