0

我想使用带有 Android Intents 的链接分享一些东西(如文本、图像等)。例如:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, someTextView.getText());
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);

此代码允许我共享文本。但我希望通过链接共享此内容,如下所示:

https://my-aplication/someText

然后,当用户进入页面时,获取当前共享文本。我怎样才能做到这一点?我调查了使用Android App Links,但我不太了解。我也试图在其他地方搜索,但我没有找到任何东西。感谢您的帮助!

4

1 回答 1

0

在调查了这个话题之后,我发现了 Google Firebase 动态链接。我看了一些关于这个话题的视频。此代码生成一个可由链接共享的密钥。首先,您必须在 Firebase 控制台中创建一个动态链接。然后,添加这些方法。这是获取动态链接数据的第一种方法:

FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent()).addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {

        @Override
        public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {

            // getting deep link
            Uri deeplink = null;
            if(pendingDynamicLinkData != null){
                deeplink = pendingDynamicLinkData.getLink();
            }

            // getting deeplink content
            if(deeplink != null)
            {
                String sharedList = deeplink.getQueryParameter("sharedList");
                userReference[0] = database.getReference(sharedList);
            }
            else userReference[0] = database.getReference().push();
            

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this, "Ooops, we couldn't get the link data :(", Toast.LENGTH_SHORT).show();
        }
    });

这是第二个,生成链接:

// generating dynamic link
private void GenerateLink(String listId) {


    System.out.println("Generating link: " + listId);
    DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse("https://www.your_dynamic_link.com/?your_variable_to_share=" + value))
            .setDomainUriPrefix("https://your_short_url.page.link")
            .setAndroidParameters(
                    new DynamicLink.AndroidParameters.Builder("com.example.your_package")
                            .setMinimumVersion(1)
                            .build())
            .buildDynamicLink();

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "I share you my list: " + dynamicLink.getUri());
    sendIntent.setType("text/plain");
    startActivity(sendIntent);

}
于 2021-04-25T19:26:18.603 回答