从菜单中自动删除快捷方式



我希望用户只能在菜单中看到我的应用程序,直到他配置了它。之后我想删除快捷方式,我也想在用户桌面上放一个快捷方式。我是java 的新手

我有办法做到这一点吗?

我不知道有什么方法可以让它出现在应用程序抽屉中,但稍后将其删除。

要在主屏幕上创建快捷方式,可以使用以下方法。

请注意-我相信此处广播的意图以及您必须在清单中请求的权限不是公共API的一部分。这种方法不会适用于每个主屏幕实现。这种方法可能随时停止工作(可能已经不适用于较新版本的android,我只在Nexus S上测试过,但它似乎在该设备上有效)。

您已收到警告

    public boolean setShortCut(Context context, String appName)
{
    System.out.println("in the shortcutapp on create method ");
    boolean flag =false ;
    int app_id=-1;
    PackageManager p = context.getPackageManager();
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> res =p.queryIntentActivities( i,0);
    System.out.println("the res size is: "+res.size());
    for(int k=0;k<res.size();k++)
    {
        System.out.println("the application name is: "+res.get(k).activityInfo.loadLabel(p));
        if(res.get(k).activityInfo.loadLabel(p).toString().equals(appName)){
            flag = true;
            app_id = k;
            break;
        }
    }
    if(flag){
        ActivityInfo ai = res.get(app_id).activityInfo;
        Intent shortcutIntent = new Intent();
        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, "SIZETESTDYNAMIC");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
        //intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        context.sendBroadcast(intent);
        System.out.println("in the shortcutapp on create method completed");
    }
    else
        System.out.println("appllicaton not found");
    return true;
}

您必须将此权限添加到您的清单中:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

最新更新