Android Create Shortcut 桌面 快捷方式 图标

Add permission:

[html]   view plain copy
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>  

Code:

[java]   view plain copy
<EMBED id=ZeroClipboardMovie_2 height=18 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
  1. private void setUpShortCut() {  
  2.   
  3.         Intent intent = new Intent(CREATE_SHORTCUT_ACTION);  
  4.   
  5.         // 设置快捷方式图片  
  6.         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.logo));  
  7.   
  8.         // 设置快捷方式名称  
  9.         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "sina");  
  10.   
  11.         // 设置是否允许重复创建快捷方式 false表示不允许  
  12.         intent.putExtra("duplicate"false);  
  13.           
  14.         // 设置快捷方式要打开的intent  
  15.         // 第一种方法创建快捷方式要打开的目标intent  
  16.         Intent targetIntent = new Intent();  
  17.         // 设置应用程序卸载时同时也删除桌面快捷方式  
  18.         targetIntent.setAction(Intent.ACTION_MAIN);  
  19.         targetIntent.addCategory("android.intent.category.LAUNCHER");  
  20.           
  21.         ComponentName componentName = new ComponentName(getPackageName(), this.getClass().getName());  
  22.         targetIntent.setComponent(componentName);  
  23.           
  24.         // 第二种方法创建快捷方式要打开的目标intent  
  25.           
  26.         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);  
  27.   
  28.         // 发送广播  
  29.         sendBroadcast(intent);  
  30. }  
  31.   
  32.       
  33.     private void tearDownShortCut() {  
  34.   
  35.         Intent intent = new Intent(DROP_SHORTCUT_ACTION);  
  36.         // 指定要删除的shortcut名称  
  37.         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "sina");  
  38.   
  39.         String appClass = getPackageName() + "." + this.getLocalClassName();  
  40.   
  41.         ComponentName component = new ComponentName(getPackageName(), appClass);  
  42.         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent().setAction(Intent.ACTION_MAIN).setComponent(component));  
  43.         sendBroadcast(intent);  
  44.   
  45. }  
 

Or:

[java]   view plain copy
<EMBED id=ZeroClipboardMovie_3 height=18 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
  1. if (Intent.ACTION_CREATE_SHORTCUT.equals(action))  
  2. {  
  3.  setupShortcut();  
  4.  finish();  
  5.  return;  
  6. }  
  7.    
  8. ...  
  9. private void setupShortcut() {  
  10.  // First, set up the shortcut intent.  
  11.  //For this example, we simply create an intent that  
  12.  // will bring us directly back to this activity.  
  13.  //A more typical implementation would use a  
  14.  // data Uri in order to display a more specific result,  
  15.  //or a custom action in order to  
  16.  // launch a specific operation.  
  17.    
  18.  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
  19.  shortcutIntent.setClassName(thisthis.getClass().getName());  
  20.  shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");  
  21.    
  22.  // Then, set up the container intent (the response to the caller)  
  23.    
  24.  Intent intent = new Intent();  
  25.  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  26.  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));  
  27.  Parcelable iconResource = Intent.ShortcutIconResource.fromContext(  
  28.  this,  R.drawable.app_sample_code);  
  29.  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);  
  30.    
  31.  // Now, return the result to the launcher  
  32.    
  33.  setResult(RESULT_OK, intent);  
  34. }  



转载于:https://my.oschina.net/u/1777508/blog/305045

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值