建立應用程式捷徑主要有以下幾種:
- 在launcher的應用程式列表上,長按某一應用程式圖示建立捷徑到案頭
- 在案頭上長按在彈出框中選擇捷徑->應用程式->將添加捷徑的程式
- 通過程式運行時自動建立
在捷徑的開發中首先要確定是否存在捷徑:一般在程式的歡迎介面及開啟程式的第一個介面的Activity的onCreate方法中添加
if (!hasShortcut()) {
Toast.makeText(mContext, "已存在捷徑", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, "無捷徑", Toast.LENGTH_LONG).show();
}
建立方法:
/**
* 判斷案頭是否已添加捷徑
*
* @param cx
* @param titleName
* 捷徑名稱
* @return
*/
public boolean hasShortcut() {
boolean result = false;
// 擷取當前應用程式名稱
String title = null;
try {
final PackageManager pm = getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = getContentResolver().query(CONTENT_URI, null,
"title=?", new String[] { title }, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
最後,添加許可權:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"
OK!判斷完成!