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

and*_*mer 6 android

我目前正在开发一个应用程序,可以创建设备的所有已安装应用程序的快捷方式.为了创建快捷方式,用户需要以管理员身份登录,并且从该活动中,有一个包含所有已安装应用程序的ListView.用户需要选择app/s以便创建快捷方式.

private boolean Generate_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) {    
        // CREATE SHORTCUT  
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");       
    } else {
        // REMOVE SHORTCUT
        intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    }

    context.sendBroadcast(intent);

} else
    System.out.println("applicaton not found");
return true;
}
Run Code Online (Sandbox Code Playgroud)

选择应用程序后,它将在Main Activity中创建一个具有GridView的快捷方式.这里的问题是创建的快捷方式是在主屏幕中创建的.现在我的问题是,如何在主屏幕中创建我的应用程序(MainActivity.java)中的快捷方式?

Cur*_*phy 3

可以在您的应用程序中显示一个图标,该图标将启动另一个应用程序。您可以使用PackageManager来发现已安装的应用程序并获取应显示的启动器图标。

  1. PackageManager从应用程序的上下文中获取。

    PackageManager pm = context.getPackageManager();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个意图,用于查询现有的已安装应用程序并将显示在启动器中。

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 查询 IntentFilter 与我们的mainIntent.

    List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 迭代返回的列表ResolveInfo以获取创建用于启动应用程序的 Intent 和显示为启动器图标的资源所需的详细信息。

    for (ResolveInfo app : apps) {
        // Create an Intent that can be use to launch the application
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name));
    
        // Get the icon to display in your UI
        Drawable icon = app.activityInfo.loadIcon(pm);
    }
    
    Run Code Online (Sandbox Code Playgroud)

更新:您可以在此处找到创建自定义启动器的说明:http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/