获取用户选择的应用包名称,以使用"Intent.ACTION_SEND"进行共享 [英] Get app package name selected by user for sharing using 'Intent.ACTION_SEND'

查看:209
本文介绍了获取用户选择的应用包名称,以使用"Intent.ACTION_SEND"进行共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码共享文本

I am using following code to share text

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
startActivity(Intent.createChooser(sharingIntent, "Share using"));

我想知道用户选择共享的应用程序的软件包名称. 我已尝试使用以下代码对<5.1>设备使用 IntentSender 来做到这一点

I want to know package name of the app chosen by user to share. I have tried doing it using IntentSender for 5.1+ devices using following code

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
sharingIntent.setType("text/plain");

Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(sharingIntent, "Share using", pendingIntent.getIntentSender());
startActivity(chooser);

以下是BroadcastReceiver

Following is the BroadcastReceiver

public class BroadcastTest extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        for (String key : intent.getExtras().keySet()) {
            Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
        }
    }
}

已引用此答案 https://stackoverflow.com/a/38342788/6053724 ,但BroadcastReceiver的onReceive()是没有被调用. 有什么我想让它起作用的东西吗?

Referred this answer https://stackoverflow.com/a/38342788/6053724 but onReceive() of BroadcastReceiver is not getting invoked. Is there anything that I am missing to get it worked?

更新:发现上述代码在5.1(api 22)上运行良好,但在6.0(api 23)上,在拾取要共享的应用程序后未立即收到广播,或者有时广播丢失了./p>

Update: Found out that above code works well on 5.1(api 22) but on 6.0(api 23) broadcast is not received immediately on picking up app for sharing or sometimes broadcast is lost.

推荐答案

我不确定这是否正是您要寻找的东西,但是有一个lib可以帮助您解决此问题:

I am not sure if this is exactly what you're looking for but there's a lib which might help you with this: https://github.com/zawadz88/material-activity-chooser

它允许您例如处理单击的应用程序(请参见

It allows you to e.g. handle the clicked app (see https://github.com/zawadz88/material-activity-chooser/blob/master/sample/src/main/java/com/github/zawadz88/sample/TrackingActivityChooserActivity.java)

这篇关于获取用户选择的应用包名称,以使用"Intent.ACTION_SEND"进行共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆