在Android中的自定义启动器内创建应用程序快捷方式

在Android中的自定义启动器内创建应用程序快捷方式,android,Android,我正在尝试开发一个自定义启动程序,它将包含我安装的应用程序(甚至apk文件)的快捷方式。我可以在应用程序之外创建快捷方式,我需要做的是在我的启动程序活动中创建它。我该怎么做 AdminActivity.java(此活动包含一个方法,该方法能够从设备创建已安装应用程序的快捷方式) private boolean installUninstall\u快捷方式(上下文上下文、字符串appName、boolean isTrue){ 布尔标志=假; int app_id=-1; PackageManage

我正在尝试开发一个自定义启动程序,它将包含我安装的应用程序(甚至apk文件)的快捷方式。我可以在应用程序之外创建快捷方式,我需要做的是在我的启动程序活动中创建它。我该怎么做

AdminActivity.java(此活动包含一个方法,该方法能够从设备创建已安装应用程序的快捷方式)

private boolean installUninstall\u快捷方式(上下文上下文、字符串appName、boolean isTrue){
布尔标志=假;
int app_id=-1;
PackageManager pm=context.getPackageManager();
意向i=新意向(意向.行动\主要);
i、 addCategory(意图.类别\发射器);
List res=pm.querytentActivities(i,0);

对于(int k=0;k您正在创建的不是像主屏幕那样的启动器,它只是一个应用程序。启动器类别指的是所有应用程序抽屉中列出的活动。我不认为应用程序可以添加自己的快捷方式,否则垃圾邮件软件可能会像这样不断地将其图标添加到您的主屏幕,并骚扰每个用户

此外,installUninstall\u快捷方式不是我们在Java中命名事物的方式!实际上,它与我遇到的任何命名约定都不匹配


如果您想模拟添加类似快捷方式的功能,那么您可以获取网格视图并在其上调用addView

对不起,我真的不知道正确的命名约定。这没关系,但这不会影响进一步研究。Java类以大写字母和camelcased命名,方法以小写字母开始,也是camelcase。资源是小写的带下划线的e应用程序可以添加自己的快捷方式。显然,这是一些应用程序用来更新其图标的机制。虽然没有记录。嘿,你解决了这个问题吗?你能接受答案或添加正确的答案来关闭此问题并将其从未回答列表中删除吗,谢谢!(你可以回答自己的问题)
private boolean installUninstall_ShortCut(Context context, String appName, boolean isTrue) {
    boolean flag =false ;
    int app_id=-1;
    PackageManager pm = context.getPackageManager();
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> res = pm.queryIntentActivities( i,0);


    for(int k=0; k<res.size(); k++) {
        if(res.get(k).activityInfo.loadLabel(pm).toString().equals(appName)){
            flag = true;
            app_id = k;

            break;
        }
    }

    if(flag) {
        ActivityInfo ai = res.get(app_id).activityInfo;

        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(ai.packageName, ai.name);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
        intent.putExtra("duplicate", false);

        if(isTrue) {        
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
            intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");       
        } else {
            intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        }

        context.sendBroadcast(intent);

    } else
        System.out.println("applicaton not found");
    return true;
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/plain_without_logo"
    tools:context=".S_2nd_MainActivity" >

<GridView
    android:id="@+id/grid_app"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnWidth="80dp"
    android:gravity="center"
    android:horizontalSpacing="15dp"
    android:numColumns="auto_fit"
    android:padding="10dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="15dp" />