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

标签 android

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

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

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;
}

activity_launcher.xml(这是快捷方式所在的 Launcher Activity 的布局。我真的不知道如何将创建的快捷方式带入此 Activity )

<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" />

最佳答案

您正在创建的不是像主屏幕那样的启动器,它只是一个应用程序。启动器类别是指在所有应用程序抽屉中列出的 Activity 。我不认为应用程序可以添加自己的快捷方式,否则垃圾邮件软件可能会像这样继续将其图标添加到您的主屏幕并惹恼每个用户。

此外,installUninstall_ShortCut 并不是我们在 Java 中命名事物的方式!实际上它不符合我遇到的任何命名约定

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

关于android - 在 Android 的自定义启动器中创建应用程序快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371491/

相关文章:

android 获取 Activity 返回 null

android - 我如何在输入 "To"文件时获得电子邮件地址的建议?

android - Android 中的屏幕录像机以编程方式,而不是屏幕截图

java - 当我想要显示我的内容时,为什么我的对话框不起作用?

android - ListFragment 因兼容性库而崩溃

android - 在后台运行 native 代码

android - 我要导入 android.provider.Calendar;

android - Kotlin - SimpleDateFormat 解析需要无限时间

java - 以编程方式更改 Android 可绘制按钮图标

android - 文件名无效 : must contain only [a-z0-9_.]