Android 應用創建桌面快捷方式

jopen 11年前發布 | 28K 次閱讀 Android Android開發 移動開發

android的快捷方式比較簡單,就是發一個系統的廣播,然后為快捷方式設置Intent---

package com.xikang.android.slimcoach.utils;
/**
 * @author huiych
 * 創建快捷方式
 * @created 2013-02-21
 * */
import android.content.Intent;
import android.os.Parcelable;

import com.xikang.android.slimcoach.AppXiKang;
import com.xikang.android.slimcoach.R;
import com.xikang.android.slimcoach.ui.AppStart;

public class ShortCutUtil {
 public static void initShortCut(){
  Intent addShortCut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  //不能重復創建快捷方式
  addShortCut.putExtra("duplicate", false);
        String title = AppXiKang.getApp().getString(R.string.app_name);//名稱  
        Parcelable icon = Intent.ShortcutIconResource.fromContext(AppXiKang.getApp(), R.drawable.icon);//圖標  
        //點擊快捷方式后操作Intent,快捷方式建立后,再次啟動該程序    
        Intent intent = new Intent(AppXiKang.getApp(), AppStart.class);   
        //設置快捷方式的標題    
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);   
        //設置快捷方式的圖標    
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);   
        //設置快捷方式對應的Intent    
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);   
      //發送廣播添加快捷方式    
        AppXiKang.getApp().sendBroadcast(addShortCut);
 }
}

AppXiKange.getApp(),是獲取Activity對象。

注意,要在清單文件中設置權限

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

這樣在希望增加快捷方式的時候,就可以給用戶一個alertdialog,提示,然后引用。就可以了。

市場上也有很多應用是在應用安裝的時候直接創建快捷方式。不過這樣的實現不是很友好。不建議使用。

來自:http://blog.csdn.net/ansionnal/article/details/8597328

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