现在Android 手机系统一般是定制加原生,一般定制就只有一层桌面,而原始和有一些定制系统是抽屉式桌面,这样就有一个需求,在安装应用的时候,需要在桌面上创建一个快捷方式。
其实很简单 。
第一步在启动页面的activity 添加该方法
private void createShortcut() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcut.putExtra("duplicate", false);//设置是否重复创建
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, BlankActivity.class);//设置第一个页面
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
这个是创建桌面快捷方式的方法
第二步 是在onCreate方法里面使用这个方法,使用之前判断桌面是否是存在了快捷方式这就需要用到如下方法。
<pre name="code" class="html"> // 判断是否已经存在快捷方式
public boolean isExistShortCut() {
boolean isInstallShortcut = false;
final ContentResolver cr = this.getContentResolver();
// 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
String authority="com.android.launcher2.settings";
int sdkInt = android.os.Build.VERSION.SDK_INT;
if (sdkInt < 8) {
// Android 2.1.x(API 7)以及以下的
authority = "com.android.launcher.settings";
} else if (sdkInt < 19) {
// Android 4.4以下
authority = "com.android.launcher2.settings";
} else {
// 4.4以及以上
authority = "com.android.launcher3.settings";
}
// final String AUTHORITY = "com.android.launcher2.settings";
final Uri CONTENT_URI = Uri.parse("content://" + authority + "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name) }, null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
System.out.println("--已经存在快捷方式");
}
if(c!=null && !c.isClosed()) c.close();
return isInstallShortcut;
}
如果就这的话,启动的时候会退出应用,最后还应该加上相应的权限。我把所有的权限都给贴出来,适用于不同的手机。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS"/>
<uses-permission android:name="org.adw.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="org.adw.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.qihoo360.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.qihoo360.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.lge.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.lge.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="net.qihoo.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="net.qihoo.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="org.adwfreak.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="org.adwfreak.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="org.adw.launcher_donut.permission.READ_SETTINGS"/>
<uses-permission android:name="org.adw.launcher_donut.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.huawei.launcher3.permission.READ_SETTINGS"/>
<uses-permission android:name="com.huawei.launcher3.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.fede.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.fede.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.sec.android.app.twlauncher.settings.READ_SETTINGS"/>
<uses-permission android:name="com.sec.android.app.twlauncher.settings.WRITE_SETTINGS"/>
<uses-permission android:name="com.anddoes.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.anddoes.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.tencent.qqlauncher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.tencent.qqlauncher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.huawei.launcher2.permission.READ_SETTINGS"/>
<uses-permission android:name="com.huawei.launcher2.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.android.mylauncher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.mylauncher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.ebproductions.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.ebproductions.android.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.lenovo.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.lenovo.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="telecom.mdesk.permission.READ_SETTINGS"/>
<uses-permission android:name="telecom.mdesk.permission.WRITE_SETTINGS"/>
<uses-permission android:name="dianxin.permission.ACCESS_LAUNCHER_DATA"/>