zoukankan      html  css  js  c++  java
  • 添加桌面快捷方式

    public void checkShortCut() {
    final String vString = getAppVersionName(MyAppTest.this);
    final SharedPreferences sp = getSharedPreferences("csw", 0);
    // 是否在桌面上添加了快捷方式
    boolean never_check_shortCut = sp.getBoolean("isShowIcon" + vString,
    false);
    // 存在快捷方式或者不允许添加,return
    if (never_check_shortCut) {
    return;
    } else {
    AlertDialog.Builder builder = new AlertDialog.Builder(MyAppTest.this);
    builder.setTitle("温馨提示");
    builder.setMessage("是否创建桌面快捷方式?");
    builder.setPositiveButton("创建", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    addShortCut();
    // 保存已经添加了快捷方式的信息,以便程序下次启动的不再提示
    Editor editor = sp.edit();
    editor.putBoolean("isShowIcon" + vString, true);
    editor.commit();
    }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

    }
    });
    builder.create().show();
    }
    }

    public void addShortCut() {
    // 添加快捷方式
    // 指定快捷方式的Action
    Intent installShortCut = new Intent(
    "com.android.launcher.action.INSTALL_SHORTCUT");
    // 添加快捷方式的名称
    installShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
    getString(R.string.app_name));
    installShortCut.putExtra("duplicate", false);

    // 下面NewAppActivity是我这边当前的activity名字,用的时候可以适当改成你自己的activity名
    ComponentName comp = new ComponentName(MyAppTest.this.getPackageName(),
    "." + MyAppTest.this.getLocalClassName());
    installShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
    Intent.ACTION_MAIN).setComponent(comp));
    // 指定图标
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
    MyAppTest.this, R.drawable.icon);
    installShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
    // 发送广播
    sendBroadcast(installShortCut);
    }

    public String getAppVersionName(Context context) {
    String versionName = "";
    try {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
    versionName = pi.versionName;
    if (versionName == null || versionName.length() <= 0) {
    return "";
    }
    } catch (Exception e) {
    Log.e("VersionInfo", "Exception", e);
    }
    return versionName;
    }

  • 相关阅读:
    Python 荣登 TIOBE 2020 年度编程语言
    火了!开源的Python抢票神器,过年回家就看这一波了!
    教你怎么用 Python 自动整理文件
    来自程序员的圣诞节浪漫-用Python画一棵带音乐的雪夜圣诞树
    用Python 绘制一个只属于你自己的世界地图
    Python黑科技:暴力破解,你的密码真的安全么?
    小白必读!十大被低估的Python自带库!
    MyEclipse提示MyEclipse Trial Expired(试用期过),破解方法
    转发和重定向的区别
    request内置对象
  • 原文地址:https://www.cnblogs.com/caishuowen/p/2362494.html
Copyright © 2011-2022 走看看