A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 田坤 中级黑马   /  2013-9-20 11:49  /  2126 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


    开发一款软件后,如果这款软件需要经常使用,那么在桌面有一个快捷方式会很方便,详细代码如下:
    1,在主界面写创建快捷方式的方法:

        
public void createDeskShortCut() {                  
  //创建快捷方式的Intent
  Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  //不允许重复创建
  shortcut.putExtra("duplicate",false);
  //需要现实的名称
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
  //快捷图片
  Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher);
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
  //快捷方式入口
  Intent intent = new Intent(getApplicationContext(),MainActivity.class);
  //下面两个属性是为了当应用程序卸载时,删除桌面上的快捷方式
  intent.setAction("android.intent.action.MAIN");
  intent.addCategory("android.intent.category.LAUNCHER");
  //点击快捷图片,运行的程序主入口
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
  //发送广播 OK
  sendBroadcast(shortcut);
}

    2,在onCreate方法中,setContentView(R.layout.activity_main)后添加如下代码:

  SharedPreferences preferences = getSharedPreferences("first",Context.MODE_PRIVATE);
  boolean isFirst = preferences.getBoolean("isfrist", true);
  if(isFirst) {
createDeskShortCut();
}
  SharedPreferences.Editor editor = preferences.edit();
  editor.putBoolean("isfrist",false);
  editor.commit();


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马