当前位置: 移动技术网 > 移动技术>移动开发>Android > Android应用创建桌面快捷方式代码

Android应用创建桌面快捷方式代码

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

android的快捷方式比较简单,就是发一个系统的广播,然后为快捷方式设置intent---

package com.xikang.android.slimcoach.utils;
/**
 * @author huiych
 * 创建快捷方式
 * @created 2013-02-21
 * */
import android.content.intent;
import android.os.parcelable;

import com.xikang.android.slimcoach.appxikang;
import com.xikang.android.slimcoach.r;
import com.xikang.android.slimcoach.ui.appstart;

public class shortcututil {
 public static void initshortcut(){
 intent addshortcut = new intent("com.android.launcher.action.install_shortcut");
 //不能重复创建快捷方式
 addshortcut.putextra("duplicate", false);
    string title = appxikang.getapp().getstring(r.string.app_name);//名称 
    parcelable icon = intent.shortcuticonresource.fromcontext(appxikang.getapp(), r.drawable.icon);//图标 
    //点击快捷方式后操作intent,快捷方式建立后,再次启动该程序  
    intent intent = new intent(appxikang.getapp(), appstart.class);  
    //设置快捷方式的标题  
    addshortcut.putextra(intent.extra_shortcut_name, title);  
    //设置快捷方式的图标  
    addshortcut.putextra(intent.extra_shortcut_icon_resource, icon);  
    //设置快捷方式对应的intent  
    addshortcut.putextra(intent.extra_shortcut_intent, intent);  
   //发送广播添加快捷方式  
    appxikang.getapp().sendbroadcast(addshortcut);
 }
}

appxikange.getapp(),是获取activity对象。

注意,要在清单文件中设置权限

复制代码 代码如下:
<uses-permission android:name="com.android.launcher.permission.install_shortcut"/>

这样在希望增加快捷方式的时候,就可以给用户一个alertdialog,提示,然后引用。就可以了。

市场上也有很多应用是在应用安装的时候直接创建快捷方式。不过这样的实现不是很友好。不建议使用。

下面上个完整的代码演示,使用的方法和上面的稍有不同:

public class shortcututil {
public static void initshortcut(activity acti){
intent shortcut = new intent("com.android.launcher.action.install_shortcut"); 
//快捷方式的名称 
shortcut.putextra(intent.extra_shortcut_name, appxikang.getapp().getstring(r.string.app_name)); 
shortcut.putextra("duplicate", false); //不允许重复创建 
//指定当前的activity为快捷方式启动的对象: 如 
//com.everest.video.videoplayer 
//注意: componentname的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序 
componentname comp = new componentname(appxikang.getapp().getpackagename(), "."+acti.getlocalclassname()); 
shortcut.putextra(intent.extra_shortcut_intent, new intent(intent.action_main).setcomponent(comp)); 
//快捷方式的图标 
shortcuticonresource iconres = intent.shortcuticonresource.fromcontext(appxikang.getapp(), r.drawable.icon); 
shortcut.putextra(intent.extra_shortcut_icon_resource, iconres); 
appxikang.getapp().sendbroadcast(shortcut);
}
public static void delshortcut(activity acti){
    intent shortcut = new intent("com.android.launcher.action.uninstall_shortcut"); 
        
    //快捷方式的名称 
    shortcut.putextra(intent.extra_shortcut_name, appxikang.getapp().getstring(r.string.app_name)); 
    string appclass = appxikang.getapp().getpackagename() + "." +acti.getlocalclassname(); 
    componentname comp = new componentname(appxikang.getapp().getpackagename(), appclass); 
    shortcut.putextra(intent.extra_shortcut_intent, new intent(intent.action_main).setcomponent(comp)); 
    appxikang.getapp().sendbroadcast(shortcut);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网