当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程实现创建,删除,判断快捷方式的方法

Android编程实现创建,删除,判断快捷方式的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

大明龙权风云再起,130130,山西仁爱医院

本文实例讲述了android编程实现创建,删除,判断快捷方式的方法。分享给大家供大家参考,具体如下:

/**
* 为程序创建桌面快捷方式 ,这样写,在程序卸载的时候,快捷方式也会一并删除
*/
private void addshortcut() {
    intent shortcutintent = new intent(
        "com.android.launcher.action.install_shortcut");
    // 快捷方式的名称
    shortcutintent.putextra(intent.extra_shortcut_name,
        getstring(r.string.app_name));
    shortcutintent.putextra("duplicate", false); // 不允许重复创建
    /*
     * shortcutintent.putextra(intent.extra_shortcut_intent, new intent(
     * getapplicationcontext(), splashactivity.class));
     */
    // 注意: componentname的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
    componentname comp = new componentname(this.getpackagename(),
        this.getpackagename() + "." + this.getlocalclassname());
    intent intent = new intent(intent.action_main);
    intent.setaction("android.intent.action.main");
    intent.addcategory("android.intent.category.launcher");
    shortcutintent.putextra(intent.extra_shortcut_intent,
        intent.setcomponent(comp));
    // 快捷方式的图标
    shortcuticonresource iconres = intent.shortcuticonresource.fromcontext(
        this, r.drawable.icon_launcher);
    shortcutintent.putextra(intent.extra_shortcut_icon_resource, iconres);
    sendbroadcast(shortcutintent);
}
//判断是否已经创建快捷方式
private boolean hasshortcut() {
    boolean isinstallshortcut = false;
    final contentresolver resolver = this.getcontentresolver();
    final string authority;
    if (android.os.build.version.sdk_int < 8) {
      authority = "com.android.launcher.settings";
    } else {
      authority = "com.android.launcher2.settings";
    }
    final uri content_uri = uri.parse("content://" + authority
        + "/favorites?notify=true");
    cursor c = resolver
        .query(content_uri,
            new string[] { "title", "iconresource" },
            "title=?",
            new string[] { this.getstring(r.string.app_name).trim() },
            null);
    if (c != null && c.getcount() > 0) {
      isinstallshortcut = true;
    }
    return isinstallshortcut;
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网