码迷,mamicode.com
首页 > 移动开发 > 详细

android开发步步为营之49:创建APP桌面快捷方式

时间:2014-11-25 18:38:43      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:android   快捷方式   



 网上参考过N多的资料,最后发现还是这样写比较靠谱,不会重复创建快捷方式。

   //创建快捷方式

    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); //不允许重复创建   

            Intent intent = new Intent(this,HomeActivity.class);//后面的HomeActivity.class是我的程序第一次加载的activity的名字,大家要注意

            intent.setAction("com.figo.activity.home");//这个也是home的具体路径

            shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

            //显示的图标

            Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher);

            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

            sendBroadcast(shortcut);//广播

        }

 

    //删除快捷方式

    private void delShortcut(){   

            Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

            //快捷方式的名称   

            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   

            

          //这里的intent要和创建时的intent设置一致

            Intent intent = new Intent(this,HomeActivity.class);

            intent.setAction("com.figo.activity.home");

               shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); 

            sendBroadcast(shortcut); 

        } 

配置文件AndroidManifest.xml

权限记得加上

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

添加上intent-filter

     <activity

            android:name="com.figo.activity.HomeActivity"

            android:launchMode="singleTask"

            android:screenOrientation="portrait"

            android:theme="@style/Theme.NoTitleBar"

            android:windowSoftInputMode="adjustPan" >

             <intent-filter>

                <action android:name="com.figo.activity.home" />

            </intent-filter>

        </activity>



android开发步步为营之49:创建APP桌面快捷方式

标签:android   快捷方式   

原文地址:http://blog.csdn.net/figo0423/article/details/41484761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!