长按android应用程序图标(如paytm和swiggy)后如何显示菜单选项?

时间:2018-11-05 10:06:39

标签: android

长按android应用程序图标(如paytm和swiggy)后如何显示菜单选项?是否有任何教程可以完成上述任务。

2 个答案:

答案 0 :(得分:0)

Shortcuts是您所需要的。

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, "id1")
    .setShortLabel("Website")
    .setLongLabel("Open the website")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.mysite.example.com/")))
    .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

答案 1 :(得分:0)

尝试以下代码。要显示您的商品,请在onLongClick()方法中添加您的代码...希望对您有所帮助...!

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);

new Handler().post(new Runnable() {
    @Override
    public void run() {
        final View view = findViewById(R.id.add_item);

        if (view != null) {
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    // Do something...

                    Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
        }
    }
});

return super.onCreateOptionsMenu(menu);

}

相关问题