Android實現類似于QQ將好友的頭像用作快捷方式

jopen 9年前發布 | 1K 次閱讀 Java Android

原因:

寫這個的原因就是網上大都是告訴你如何用drawable里的資源做快捷方式,那么本地的圖片能不能做快捷方式呢?肯定是可以的,所以我找了下安卓的源碼,發現有這樣的介紹

**

  • Activity Action: Creates a shortcut.
  • <p>Input: Nothing.</p>
  • <p>Output: An Intent representing the shortcut. The intent must contain three
  • extras: SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String),
  • and SHORTCUT_ICON (value: Bitmap) or SHORTCUT_ICON_RESOURCE
  • (value: ShortcutIconResource).</p>
  • @see #EXTRA_SHORTCUT_INTENT
  • @see #EXTRA_SHORTCUT_NAME
  • @see #EXTRA_SHORTCUT_ICON
  • @see #EXTRA_SHORTCUT_ICON_RESOURCE
  • @see android.content.Intent.ShortcutIconResource */ </pre>
    不難看出有兩種方式創建快捷方式圖標,不過大都用的第一種。所以我們就試試第二種吧。

    解決方案:

    既然都看了源碼的介紹了,自然就知道怎么做了。
        public void CreateShotCast(Context context, Class<?> clazz) {

     Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);  
    
     Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
     shortcutIntent.setClass(context, clazz);  
    
     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "測試");  
     // Intent.EXTRA_SHORTCUT_ICON 是bitmap對象  
     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeFile("路徑"));  
     context.sendBroadcast(shortcut);  
    

    } </pre>
    來自:http://blog.csdn.net/windowsxp2014/article/details/45822757

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!