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

Android开启应用时自动创建快捷方式

时间:2014-12-22 14:30:41      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:android   快捷方式   

     今天在使用360手机助手时,发现它会自动创建一个快捷启动方式,感觉挺有意思的,所以就上网百度了一些方法,试了一下,有一种方法是挺不错的,我借鉴来用用,先保存下来,我也写了一个测试小程序。

     首先,在你要启动应用的mainActivity里添加代码,在onCreate()方法里,加一个方法createShortCut();

然后就是方法的实现:

private void createShortCut() {
        // TODO Auto-generated method stub
        //创建快捷方式的Intent
         addShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
        //不允许重复创建
        addShortcut.putExtra("duplicate", false);
        //<span><span class="comment">指定当前的Activity为快捷方式启动的对象: 如 com.android.music.</span>MusicBrowserActivity<span> 
</span></span>        //<span><span class="comment">注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序</span><span></span></span>
         ComponentName comp = new ComponentName(this.getPackageName(), "."
                + this.getLocalClassName());
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(Intent.ACTION_MAIN).setComponent(comp));
        //设置快捷方式的图标
        ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this,
                R.drawable.ic_launcher);
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
       // 设置快捷方式的名字
       addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        sendBroadcast(addShortcut);
}
这个当然要添加一个权限才能成功:在AndroidManifest.xml里添加

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
OK,现在就可以运行程序了!

Android开启应用时自动创建快捷方式

标签:android   快捷方式   

原文地址:http://blog.csdn.net/tfslovexizi/article/details/42079141

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