[Android]為指定的應用建立案頭捷徑

來源:互聯網
上載者:User

    網上一搜一大把為自己的應用建立捷徑,但是本文的側重點在為“指定的應用”建立案頭捷徑。
    常見的案頭捷徑有兩要素:1.應用程式名稱 2.應用表徵圖。

    指定應用表徵圖的資訊是:

    // pkgContext為指定應用的上下文環境,iconIdentifier為一個整數,指定應用的表徵圖標識符
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(pkgContext,
iconIdentifier);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

 

    建立第三方應用的捷徑關鍵在於如何擷取第三方應用的上下文環境,關鍵代碼為:

    Context pkgContext = context.createPackageContext(pkg, Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);

 

   OK,基礎知識講完了,下面直接給代碼以饗讀者。

        /**
* @param context
* 執行者。
* @params pkg 待添加捷徑的應用程式套件名,其值不可為null。
* @return 返回true為正常執行完畢,<br/>
* 返回false為pkg值為null或者找不到該應用或者該應用無用於Launch的MainActivity 。
* @author sodino
* */
public boolean addShortcut(Context context, String pkg) {
// 捷徑名
String title = "unknown";
// MainActivity完整名
String mainAct = null;
// 應用表徵圖標識
int iconIdentifier = 0;
// 根據包名尋找MainActivity
PackageManager pkgMag = context.getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_MAIN, null);
queryIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = pkgMag.queryIntentActivities(queryIntent,
PackageManager.GET_ACTIVITIES);
for (int i = 0; i < list.size(); i++) {
ResolveInfo info = list.get(i);
if (info.activityInfo.packageName.equals(pkg)) {
title = info.loadLabel(pkgMag).toString();
mainAct = info.activityInfo.name;
iconIdentifier = info.activityInfo.applicationInfo.icon;
break;
}
}
if (mainAct == null) {
// 沒有啟動類
return false;
}
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 捷徑的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
//不允許重複建立
// shortcut.putExtra("duplicate", false);
ComponentName comp = new ComponentName(pkg, mainAct);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(Intent.ACTION_MAIN).setComponent(comp));
// 捷徑的表徵圖
Context pkgContext = null;
if (context.getPackageName().equals(pkg)) {
pkgContext = context;
} else {
// 建立第三方應用的上下文環境,為的是能夠根據該應用的表徵圖標識符尋找到表徵圖檔案。
try {
pkgContext = context.createPackageContext(pkg, Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
if (pkgContext != null) {
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(pkgContext,
iconIdentifier);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
}
// 發送廣播,讓接收者建立捷徑
// 需許可權<uses-permission
// android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
context.sendBroadcast(shortcut);
return true;
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.