Android,通过 Sharesheet 共享自定义数据?

Android, Share custom data via Sharesheet?

我正在尝试使用 Sharesheet 共享自定义数据,第一个问题是:这可能吗?

示例:在我的应用程序中有一个地址簿,我想通过 Whatsapp 与朋友分享一个联系人 [class:联系人]。当他收到消息时,他应该能够直接在应用程序中打开联系人,或者,如果没有安装,他应该被引导到 Play 商店下载应用程序。

我找到了发送简单文本的方法,并且我已经为每个 class 构建了我想分享相应的 Parcelable。 现在以下是我的代码,Sharesheet 正在打开,但是如果我 select 我想与之共享的联系人给我一条错误消息:“共享失败,重试”(Whatsapp)或“不支持的内容”(Telegram) .

sContact = new Contact();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
ArrayList<Contact> ContactsList = new ArrayList<Contact>();
ContactsList.add(sContact);
Parcel.obtain().readList(ContactsList, Contact.class.getClassLoader());
sendIntent.putParcelableArrayListExtra("ShareContact", ContactsList);
sendIntent.setType("image/*");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);

我无法理解的另一件事是,如果我向 ContactsList 添加一个项目,应用程序会崩溃并出现错误“android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.myapp.Contact”,而不是列表为空共享单打开。

is it possible?

就您所说的“自定义数据”而言,不。

When he receive the message he should be able to open the contact directly in the app or, if it's not installed, he should be directed to the play store to download the app.

您可以将 https URL 共享到您的 Web 服务器,其中 URL 包含联系人的不透明标识符。您可以让您的应用程序支持该 URL 结构的深层 link,这样如果用户安装了该应用程序,单击 link 可能会打开您的应用程序(您可以在其中解码 URL 并使用标识符)。您的 Web 服务器可以为实际页面做任何它想做的事情,包括如果 Web 服务器认为发出请求的客户端是 Android Web 浏览器,则向您的应用 market: URL 发出重定向。

Now the following is my code, the Sharesheet is opening but if I select the contact I want to share with it give me an error message

首先,其他应用没有你的Contactclass。如果你把一个放在你的 Intent 中,它们会崩溃并显示 ClassNotFoundException

其次,您没有关注 the rules for ACTION_SEND。当其他应用程序尝试使用您的 Intent 时,即使 Contact 不是问题,它们也会失败,因为它们会尝试使用 EXTRA_TEXTEXTRA_STREAM,而您却这样做了没有。而且他们会尝试使用 image/* 作为 MIME 类型,这不仅无法匹配您的 non-existent 文本或流,而且还是通配符,您需要提供具体的 MIME 类型。