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

6

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

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

选定一个应用后,它会在具有 GridView 的 Main Activity 中创建一个快捷方式。问题在于创建的快捷方式是在主屏幕中创建的。现在我的问题是,如何在应用程序(MainActivity.java)内部创建快捷方式,而不是在主屏幕上创建?


你能详细说明一下“在我目前正在开发的应用程序中创建一个快捷方式”吗?你是想创建一种启动器吗? - Andrew T.
@AndrewT.,是的,这就是我想做的。我想在我的应用程序内创建一个启动器。 - androidBoomer
由于快捷方式在您自己的应用程序内部,因此可以按照您希望的任何方式进行操作。只需存储您已经向用户显示的信息并使用它即可。 - android developer
2个回答

4

你试过了吗?

<activity android:name=".YourActivity" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

在接收 intent 的活动中,您需要创建一个与您的快捷方式相关的意图,并将其作为活动结果返回。

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);

3

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

  1. Get the PackageManager from your application's context.

    PackageManager pm = context.getPackageManager();
    
  2. Create an intent that will be used to query for the installed applications that exist and would be displayed in the launcher.

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
  3. Query the for any applications whose IntentFilter matches our mainIntent.

    List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
    
  4. Iterate the List of ResolveInfo that is returned to obtain the details necessary to create an Intent for launching the application and the resource to display as the launcher icon.

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

更新: 您可以在这里找到创建自定义启动器的详细步骤:http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/


顺便问一下,在您回答的最后一部分中,我应该在哪里使用“Drawable图标”? - androidBoomer
你需要使用图标可绘制对象来展示应用内的快捷方式。你需要实现用户界面。对于自定义启动器,我过去使用过GridView - csmurphy84
你能提供相关代码吗?我真的很需要尽快完成它。 - androidBoomer
请查看我更新的答案中的博客文章链接。它应该有助于设置用户界面。 - csmurphy84
完全看不懂,博主的代码中出现了这么多错误。 :( - androidBoomer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接