Pay special attention to the following questions about the inability to create shortcuts:
In many cases, the meaning of this sentence is to prevent duplicate creation of shortcuts, but the problem I encountered today is that this sentence makes it impossible to create shortcuts on the desktop.
My approach is:
1. Use SharedPreferences to determine whether the application is started for the first time.
2. Use isInstallShortcut () to determine whether there is a shortcut
3. Create a shortcut (
private void createShortCut() { SharedPreferences userini = getSharedPreferences( getString(R.string.useriniXML), Activity.MODE_PRIVATE); Log.e("H3c","sh.."+userini.getInt("seflshotcut", 0)); if (userini.getInt("seflshotcut", 0) == 0) { m_tool.createShortCut(Main.this); SharedPreferences.Editor sEdi = userini.edit(); sEdi.putInt("seflshotcut", 1); sEdi.commit(); } }
private boolean isInstallShortcut() { boolean isInstallShortcut = false; final ContentResolver cr = MarketApplication .getMarketApplicationContext().getContentResolver(); String AUTHORITY = "com.android.launcher.settings"; Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { "XXXX" }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } if (c != null) { c.close(); } if (isInstallShortcut) { return isInstallShortcut; } AUTHORITY = "com.android.launcher2.settings"; CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { "XXXX" }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } return isInstallShortcut; }
3.
/*** Create shortcuts **/public void createShortCut (Context contxt) {if (isInstallShortcut () {// if it has been created once, no return will be created ;} intent sIntent = new Intent (Intent. ACTION_MAIN); sIntent. addCategory (Intent. CATEGORY_LAUNCHER); // after adding action and category, the sIntent is automatically uninstalled when the program is uninstalled. setClass (contxt, Login. class); Intent installer = new Intent (); // installer. putExtra ("duplicate", false); installer. putExtra ("android. intent. extra. shortcut cut. INTENT ", sIntent); installer. putExtra ("android. intent. extra. shortcut cut. NAME "," XXXX "); installer. putExtra ("android. intent. extra. shortcut cut. ICON_RESOURCE ", Intent. using cuticonresource. fromContext (contxt, R. drawable. icon); installer. setAction ("com. android. launcher. action. INSTALL_SHORTCUT "); contxt. sendBroadcast (installer );}
Test: Click