如何创建一个意图,每次点击时都会给您提供建议。

huangapple go评论106阅读模式
英文:

How to create an Intent that gives you suggestion every time you click

问题

我正在开发一个应用程序,在这个应用程序中,当用户点击共享按钮时,建议窗口将会打开,类似于Messenger、WhatsApp、蓝牙等。

但是当我从建议中选择一个选项,然后下次再点击共享按钮时,它不会提供任何建议,而是直接打开我上次从建议中选择的应用程序。我已经清除了应用程序缓存和应用程序数据,但没有效果,仍然直接打开我上次从建议中选择的应用程序。

共享按钮代码:

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, UserStatusforshare);
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
whatsappIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

try {
    context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {

    Toast.makeText(context, "Whatsapp未安装。", Toast.LENGTH_SHORT).show();
}

我有另一段代码:

Uri imgUri = Uri.parse(postImage);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
whatsappIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

try {
    context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {

    Toast.makeText(context, "没有支持的应用程序!", Toast.LENGTH_SHORT).show();
}

在这段代码中,有一个图片,我也分享了一个图片... 但是工作方式相同,每次点击时都不提供建议,直接打开我上次从建议中选择的应用程序。

我尝试了一些来自StackOverflow的答案,但我无法理解,而且我的搜索结果也不太好... 请帮助我,我是Android新手,提前感谢。

英文:

I am developing an app, where I am using a Share Button whenever the user clicks on the share button the suggestion window will be opened like Messenger, WhatsApp, Bluetooth, etc.

But when I chose from suggestions, and the next time I click on share button it doesn't provide any suggestion but take me to the app which I chose last time from suggestion I cleared app Cache and App data also but doesn't work, direct me to the app that I chose from suggestion last time.

Share Button Code:

  Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
                whatsappIntent.setType("text/plain");
                whatsappIntent.putExtra(Intent.EXTRA_TEXT, UserStatusforshare);
                whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                whatsappIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

                try {
                    context.startActivity(whatsappIntent);
                } catch (android.content.ActivityNotFoundException ex) {

                    Toast.makeText(context, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
                }

Alternate Code I have:

Uri imgUri = Uri.parse(postImage);
            Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
            whatsappIntent.setType("text/plain");
            whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
            whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
            whatsappIntent.setType("image/jpeg");
            whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            whatsappIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

            try {
                context.startActivity(whatsappIntent);
            } catch (android.content.ActivityNotFoundException ex) {

                Toast.makeText(context, "No supported app exists!", Toast.LENGTH_SHORT).show();
            }

Here in this code, there is one thing image here I am sharing an Image also... But that one work the same way doesn't provide suggestions every time click, direct me to the app I chose from the last only suggestions.

I've tried some answer from StackOverflow, but I am unable to understand and my search is also not very good... Please Help I am new to android, Thanks in Adnvance

答案1

得分: 0

请尝试使用以下代码启动意图:

startActivity(Intent.createChooser(whatsappIntent, "您想要显示的标题"));
英文:

Please try to start intent with this code :

       startActivity(Intent.createChooser(whatsappIntent, "The title what you want to show"));

huangapple
  • 本文由 发表于 2020年8月3日 16:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定