添加桌面快捷方式

来源:互联网 发布:河南网络电视大象直播 编辑:程序博客网 时间:2024/04/29 10:32

1:java代码:

  1. // 要添加的快捷方式的Intent   
  2.         Intent addShortcut;   
  3.         // 判断是否要添加快捷方式   
  4.         if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {   
  5.             addShortcut = new Intent();   
  6.             // 设置快捷方式的名字   
  7.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "发送邮件");   
  8.             // 构建快捷方式中专门的图标   
  9.             Parcelable icon = Intent.ShortcutIconResource.fromContext(this,   
  10.                     R.drawable.mail_edit);   
  11.             // 添加快捷方式图标   
  12.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);   
  13.             // 构建快捷方式执行的Intent   
  14.             Intent mailto = new Intent(Intent.ACTION_SENDTO,   
  15.                     Uri.parse("mailto:xxx@xxx.com"));   
  16.             // 添加快捷方式Intent   
  17.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto);   
  18.             // 正常   
  19.             setResult(RESULT_OK, addShortcut);   
  20.         } else {   
  21.             // 取消   
  22.             setResult(RESULT_CANCELED);   
  23.         }   
  24.         // 关闭   
  25.         finish();  

2:在AndroidManifest.xml里的内容为:

  1. <activity android:name=".Activity01" android:label="@string/app_name">   
  2.     <intent-filter>   
  3.         <action android:name="android.intent.action.MAIN" />   
  4.         <category android:name="android.intent.category.LAUNCHER" />   
  5.         <action android:name="android.intent.action.CREATE_SHORTCUT" />   
  6.     </intent-filter>   
  7. </activity>  

原创粉丝点击