1. 在清单文件里面进行注册:例如:
- <activity
- android:name="com.android.master.legend.widget.CreateSystemSettingsWidgetActivity"
- android:exported="true"
- android:icon="@drawable/ic_switcher_shortcut"
- android:label="@string/system_switcher_shortcut"
- android:screenOrientation="portrait"
- android:theme="@android:style/Theme.Translucent.NoTitleBar" >
- <intent-filter>
- <action android:name="android.intent.action.CREATE_SHORTCUT" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
2. 手动创建快捷方式
- public static void createSystemSwitcherShortCut(Context context) {
- final Intent addIntent = new Intent(
- "com.android.launcher.action.INSTALL_SHORTCUT");
- final Parcelable icon = Intent.ShortcutIconResource.fromContext(
- context, R.drawable.ic_switcher_shortcut); // 获取快捷键的图标
- addIntent.putExtra("duplicate", false);
- final Intent myIntent = new Intent(context,
- SystemSwitcherActivity.class);
- myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- context.getString(R.string.switch_widget));// 快捷方式的标题
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作
- context.sendBroadcast(addIntent);
- }
- /**
- * 为程序创建桌面快捷方式
- */
- private void addShortcut(){
- 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); //不允许重复创建
- /****************************此方法已失效*************************/
- //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());
- //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
- /******************************end*******************************/
- Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
- shortcutIntent.setClassName(this, this.getClass().getName());
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- //快捷方式的图标
- ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
- sendBroadcast(shortcut);
- }
小结:一般做法是在设置里面加上手动创建快捷方式的设置。
在程序第一次启动的时候,手动创建一次快捷方式。
3.判断是否已经创建了快捷方式(在某些机型中需要判断)
- private boolean hasShortcut()
- {
- boolean isInstallShortcut = false;
- final ContentResolver cr = activity.getContentResolver();
- final String AUTHORITY ="com.android.launcher.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[] {mapViewActivity.getString(R.string.app_name).trim()}, null);
- if(c!=null && c.getCount()>0){
- isInstallShortcut = true ;
- }
- return isInstallShortcut ;
4.删除
- /**
- * 删除程序的快捷方式
- */
- private void delShortcut(){
- Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
- String appClass = this.getPackageName() + "." +this.getLocalClassName();
- ComponentName comp = new ComponentName(this.getPackageName(), appClass);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
- sendBroadcast(shortcut);
- }
5.声明权限
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
- <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
6.一个应用开启多个的问题
现在都很流行在桌面上为自己的应用创建快捷方式,网上也很多例子,但是基本都是同一个,创建快捷方式的手法是对的,但是通过快捷方式开启自己的应用的时候会发现程序菜单里头打开的应用和桌面快捷方式打开的应用竟然不是同一个,这样会导致一个应用开启了多个。
一开始从activity的加载模式入手,把默认的activity的加载模式设置成了android:launchMode="singleInstance" 虽然每次都只开启了同一个页面,但是每次都是从新加载的,而不是直接调到已经开启的activity,这个交互很不理想;
经过仔细研究log发现,通过桌面快捷方式开启应用和通过程序菜单开启应用的日志信息差了一个东西cat=[android.intent.category.LAUNCHER],通过快捷方式开启应用并没有打印出这个属性,所以就尝试在快捷方式创建的时候给他的intent添加一个属性,
其中,关键的就是intent.addCategory(Intent.CATEGORY_LAUNCHER); 解决了标题提到的问题!
一开始从activity的加载模式入手,把默认的activity的加载模式设置成了android:launchMode="singleInstance" 虽然每次都只开启了同一个页面,但是每次都是从新加载的,而不是直接调到已经开启的activity,这个交互很不理想;
经过仔细研究log发现,通过桌面快捷方式开启应用和通过程序菜单开启应用的日志信息差了一个东西cat=[android.intent.category.LAUNCHER],通过快捷方式开启应用并没有打印出这个属性,所以就尝试在快捷方式创建的时候给他的intent添加一个属性,
- ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());
- Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
其中,关键的就是intent.addCategory(Intent.CATEGORY_LAUNCHER); 解决了标题提到的问题!
public void createShortCut() {
if (hasShortcut(this)) {return;
}
//创建快捷方式的Intent
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建
shortcutintent.putExtra("duplicate", false);
//需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
//快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//点击快捷图片,运行的程序主入口
Intent intent = new Intent();
intent.setComponent(new ComponentName(getApplicationContext(), getClass()));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(0x10200000);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//发送广播。OK
sendBroadcast(shortcutintent);
}
/**
* 判断桌面是否已添加快捷方式
*
* @param context
* @param titleName 快捷方式名称
* @return
*/
public static boolean hasShortcut(Context context) {
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA))
.toString();
} catch (Exception e) {
e.printStackTrace();
return false;
}
final Uri CONTENT_URI = Uri
.parse("content://com.android.launcher2.settings/favorites?notify=true");
final Cursor c = context.getContentResolver().query(CONTENT_URI, null, "title=?",
new String[] { title }, null);
if (c != null) {
boolean result = c.getCount() > 0;
c.close();
return result;
}
return false;
}