Android中为你的应用程序添加桌面快捷方式

来源:互联网 发布:手机人肉搜索软件 编辑:程序博客网 时间:2024/04/27 22:11

如需转载引用请注明出处:http://blog.csdn.net/jiahui524

 

相信大家在使用很多的应用的时候都碰到过这样的一个情景,那就是当我们第一次点击使用这个应用的时候程序会主动的为我们创建一个桌面上的快捷方式,这样的好处是可以让用户更加方便的进入这个应用,省去找的麻烦。有句话曾经说,谁占据了PC桌面客户端谁就是王者,像QQ,360等都是。所以我们在做开发的时候肯定也会有用到这一点,那么今天我就和大家分享实现的这样的一个功能。其实不难,你只需要在你的应用程序启动的第一个Activity里添加这样的一个方法:

[java] view plaincopyprint?
  1. /** 
  2.  * 创建快捷方式 
  3.  */  
  4. public void createDeskShortCut() {  
  5.   
  6.     Log.i("coder""------createShortCut--------");  
  7.     // 创建快捷方式的Intent  
  8.     Intent shortcutIntent = new Intent(  
  9.             "com.android.launcher.action.INSTALL_SHORTCUT");  
  10.     // 不允许重复创建  
  11.     shortcutIntent.putExtra("duplicate"false);  
  12.     // 需要现实的名称  
  13.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
  14.             getString(R.string.app_name));  
  15.   
  16.     // 快捷图片  
  17.     Parcelable icon = Intent.ShortcutIconResource.fromContext(  
  18.             getApplicationContext(), R.drawable.ic_launcher);  
  19.   
  20.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
  21.   
  22.     Intent intent = new Intent(getApplicationContext(),  
  23.             AndroidLayoutActivity.class);  
  24.   
  25.     // 点击快捷图片,运行的程序主入口  
  26.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  27.     // 发送广播。OK  
  28.     sendBroadcast(shortcutIntent);  
  29. }  

如果只是添加这些代码,当你卸装你的应用程序的时候你又会发现存在一个问题就是你的应用程序虽然卸载了,可是桌面上的快捷方式并未卸载。呵呵,其实你只要在你要设置对应启动进入的那个Intent加上这么下面的两个属性就是表明与你的应用绑定了。

[java] view plaincopyprint?
  1. // 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除  
  2.       intent.setAction("android.intent.action.MAIN");  
  3.       intent.addCategory("android.intent.category.LAUNCHER");  

给上完整的代码:

[java] view plaincopyprint?
  1. package com.jiahui.layout;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.SharedPreferences;  
  7. import android.os.Bundle;  
  8. import android.os.Parcelable;  
  9. import android.util.Log;  
  10.    
  11. public class AndroidLayoutActivity extendsActivity {  
  12.        /**Called when the activity is first created. */  
  13.        @Override  
  14.        publicvoid onCreate(Bundle savedInstanceState) {  
  15.               super.onCreate(savedInstanceState);  
  16.               setContentView(R.layout.view_personal_info);  
  17.               SharedPreferencespreferences = getSharedPreferences("first",  
  18.                             Context.MODE_PRIVATE);  
  19.               booleanisFirst = preferences.getBoolean("isfrist"true);  
  20.               if(isFirst) {  
  21.                      createDeskShortCut();  
  22.               }  
  23.               SharedPreferences.Editoreditor = preferences.edit();  
  24.               editor.putBoolean("isfrist",false);  
  25.               editor.commit();  
  26.        }  
  27.    
  28.        /** 
  29.         * 创建快捷方式 
  30.         */  
  31.        publicvoid createDeskShortCut() {  
  32.    
  33.               Log.i("coder","------createShortCut--------");  
  34.               //创建快捷方式的Intent  
  35.               IntentshortcutIntent = new Intent(  
  36.                             "com.android.launcher.action.INSTALL_SHORTCUT");  
  37.               //不允许重复创建  
  38.               shortcutIntent.putExtra("duplicate",false);  
  39.               //需要现实的名称  
  40.               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
  41.                             getString(R.string.app_name));  
  42.    
  43.               //快捷图片  
  44.               Parcelableicon = Intent.ShortcutIconResource.fromContext(  
  45.                             getApplicationContext(),R.drawable.ic_launcher);  
  46.    
  47.               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);  
  48.    
  49.               Intentintent = new Intent(getApplicationContext(),  
  50.                             AndroidLayoutActivity.class);  
  51.               //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除  
  52.               intent.setAction("android.intent.action.MAIN");  
  53.               intent.addCategory("android.intent.category.LAUNCHER");  
  54.               //点击快捷图片,运行的程序主入口  
  55.               shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  
  56.               //发送广播。OK  
  57.               sendBroadcast(shortcutIntent);  
  58.        }  
  59. }  

哦,亲,千万别忘记在AndroidManifest.xml加上下面的这个权限:

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

 


最终效果图:

 


0 0
原创粉丝点击