现在的位置: 首页 > 综合 > 正文

总结:android 创建快捷方式的两种方式+判断是否已经创建+删除快捷方式

2013年12月06日 ⁄ 综合 ⁄ 共 4118字 ⁄ 字号 评论关闭

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>

这样,就会自动加入到 系统launcher的快捷方式里面

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.声明权限

在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限

<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添加一个属性,
 
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);  完美解决了标题提到的问题!

抱歉!评论已关闭.