android应用添加快捷方式

第一次运行程序添加快捷方式,我是在SharedPreferences里面添加了一个字段标记是不是第一次运行。

我在Application的实现类的onCreate方法中判断

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public void onCreate() {  
  3.         super.onCreate();  
  4.   
  5.         appContext = getApplicationContext();  
  6.   
  7.         SharedPreferences shortcutpref = appContext.getSharedPreferences(  
  8.                 "shortcut", Context.MODE_PRIVATE);  
  9.   
  10.         boolean iscreated = shortcutpref.getBoolean("iscreated"false);  
  11.   
  12.         if (!iscreated) {  
  13.             createDeskShortCut();  
  14.         }  


下面就是createDeskShortCut的实现:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 创建快捷方式 
  3.      */  
  4.     public void createDeskShortCut() {  
  5.         // 创建快捷方式的Intent  
  6.         Intent shortcutIntent = new Intent(  
  7.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  8.         // 不允许重复创建  
  9.         shortcutIntent.putExtra("duplicate"false);  
  10.         // 需要显示的名称  
  11.         shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
  12.                 getString(R.string.app_name));  
  13.   
  14.         // 快捷图片  
  15.         Parcelable icon = Intent.ShortcutIconResource.fromContext(  
  16.                 getApplicationContext(), R.drawable.logo);  
  17.   
  18.         shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
  19.   
  20.         Intent intent = new Intent(getApplicationContext(),  
  21.                 WelcomeActivity.class);  
  22.         // 下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除  
  23.         intent.setAction("android.intent.action.MAIN");  
  24.         intent.addCategory("android.intent.category.LAUNCHER");  
  25.         // 点击快捷图片,运行的程序主入口  
  26.         shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  27.         // 发送广播。OK  
  28.         sendBroadcast(shortcutIntent);  
  29.   
  30.         // 在配置文件中声明已经创建了快捷方式  
  31.         appContext.getSharedPreferences("shortcut", Context.MODE_PRIVATE)  
  32.                 .edit().putBoolean("iscreated"true).commit();  
  33.   
  34.         // 2.3.3系统创建快捷方式不提示  
  35.         if (android.os.Build.VERSION.SDK.equals("10")) {  
  36.             Toast.makeText(  
  37.                     appContext,  
  38.                     "已创建"  
  39.                             + appContext.getResources().getString(  
  40.                                     R.string.app_name) + "快捷方式。",  
  41.                     Toast.LENGTH_LONG).show();  
  42.         }  
  43.   
  44.         String handSetInfo = "手机型号:" + android.os.Build.MODEL + ",SDK版本:"  
  45.                 + android.os.Build.VERSION.SDK + ",系统版本:"  
  46.                 + android.os.Build.VERSION.RELEASE;  
  47.         Log.e("HANDINFO", handSetInfo);  
  48.     }  

这种方法我测试了4.0以上的都没有问题,都是可以Toast提示创建的,但是用2.3.3运行的时候 快捷方式创建了,但是没有提示。

创建快捷方式是发一个广播,然后由系统为您创建,创建成功会自动提示,但是可能有兼容问题导致2.3.3没有提示。这里就手动添加以下了。


上面获取SDK版本 和 系统版本都是很常用的,记录以下:

"手机型号:"  android.os.Build.MODEL 

"SDK版本:"  android.os.Build.VERSION.SDK  

"系统版本:" android.os.Build.VERSION.RELEASE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值