Android中建立和檢測捷徑

來源:互聯網
上載者:User

public class ShortcutUtil {</p><p>/**<br /> * 建立捷徑<br /> *<br /> * @param context<br /> */<br />public static void createShortcut(Context context, Class<?> target) {<br />// <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><br />Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");<br />intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));<br />intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));<br />intent.putExtra("duplicate", false);<br />Intent sIntent = new Intent(Intent.ACTION_MAIN);<br />sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之後,程式卸載的時候才會主動將該捷徑也卸載<br />sIntent.setClass(context, target);<br />intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);<br />context.sendBroadcast(intent);<br />}</p><p>/**<br /> * 判斷案頭上是否有的捷徑<br /> *<br /> * @return<br /> */<br />public static boolean hasShortcut(Context context) {<br />final ContentResolver cr = context.getContentResolver();<br />final String AUTHORITY = "com.android.launcher.settings";<br />final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");<br />// <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/><br />Cursor c = cr.query(CONTENT_URI, new String[] { "intent" }, null, null, null);<br />if (c == null) {<br />// 注: 2.1update和2.2版本的真機上測試無法訪問com.android.launcher.settings,2.1update1的模擬器上可以<br />// ERROR/ActivityThread(1136): Failed to find provider info for com.android.launcher.settings<br />return false;<br />}<br />while (c.moveToNext()) {<br />String intentstring = c.getString(c.getColumnIndex("intent"));<br />if (intentstring == null) {<br />continue;<br />}<br />String componentString = getComponentString(intentstring);<br />if (componentString.startsWith(context.getPackageName())) {<br />return true;<br />}<br />}<br />return false;<br />}</p><p>private static String getComponentString(String intentInfo) {<br />// intent info 的格式:intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.allstar.tanzhi/.activities.StartActivity;end<br />int start = intentInfo.indexOf("component") + 9 + 1;<br />int end = intentInfo.indexOf(";", start);<br />return intentInfo.substring(start, end);<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.