在 Android 中共享应用程序“链接”

我希望我的应用程序用户能够共享/推荐我的应用程序给其他用户。我使用 ACTION _ SEND 意图。我添加了纯文本,内容大致如下: 安装这个很酷的应用程序。但是,我找不到一种方法,使用户可以直接到安装屏幕的市场,例如。我能提供给他们的只是一个网络链接或一些文本。 换句话说,我正在寻找一个非常直接的方式为 android 用户有我的应用程序安装。

谢谢你的指点,

托马斯

161328 次浏览

托马斯,

You would want to provide your users with a market:// link which will bring them directly to the details page of your app. The following is from developer.android.com:

加载应用程序的详细信息页

在 Android 市场,每一个应用程序 具有一个详细信息页,该页提供一个 用户应用程序概述。 例如,该页包括一个简短的 应用程序和屏幕的描述 使用中的它的镜头,如果提供 开发人员,以及反馈 用户和有关 详细信息页面 包括一个“安装”按钮,让 用户触发下载/购买 申请书。

如果要将用户引用到 specific application, your 应用程序可以直接采取用户 到应用程序的详细信息页 这样做,您的应用程序将发送一个 ACTION_VIEW Intent that includes a URI 和查询参数的格式如下:

市场://详情? id =

在这种情况下,包的名称 parameter is target application's 完全限定的包名称,如 的包属性中声明的 中的清单元素 application's manifest file. For 例如:

Market://Details? id = com.example.android.jetboy

资料来源: http://developer.android.com/guide/publishing/publishing.html

更准确地说

   Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.android.example"));
startActivity(intent);

or if you want to share your other apps from your dev. account you can do something like this

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Your_Publisher_Name"));
startActivity(intent);

This will let you choose from email, whatsapp or whatever.

try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
String shareMessage= "\nLet me recommend you this application\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) {
//e.toString();
}

要自动填写应用程序名称和应用程序 ID,您可以使用以下命令:

int applicationNameId = context.getApplicationInfo().labelRes;
final String appPackageName = context.getPackageName();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, activity.getString(applicationNameId));
String text = "Install this cool application: ";
String link = "https://play.google.com/store/apps/details?id=" + appPackageName;
i.putExtra(Intent.EXTRA_TEXT, text + " " + link);
startActivity(Intent.createChooser(i, "Share link:"));

把这种方法称为:

public static void shareApp(Context context)
{
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
sendIntent.setType("text/plain");
context.startActivity(sendIntent);
}

我知道这个问题已经得到了回答,但我想分享另一种解决办法:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareSubText = "WhatsApp - The Great Chat App";
String shareBodyText = "https://play.google.com/store/apps/details?id=com.whatsapp&hl=en";
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubText);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(shareIntent, "Share With"));

Share application with title is you app_name, content is your application link

fun shareApp(context: Context) {
val appPackageName = BuildConfig.APPLICATION_ID
val appName = context.getString(R.string.app_name)
val shareBodyText = "https://play.google.com/store/apps/details?id=$appPackageName"


val sendIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TITLE, appName)
putExtra(Intent.EXTRA_TEXT, shareBodyText)
}
context.startActivity(Intent.createChooser(sendIntent, null))
}

最后,这段代码是为我打开的电子邮件客户端从安卓设备。 try this snippet.

Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "Feedback" + "&body=" + "Write Feedback here....." + "&to=" + "someone@example.com");
testIntent.setData(data);
startActivity(testIntent);

实际上,在用户之间剪切你的应用程序的最好方式,google (Firebase)证明了新技术 Firebase 动态链接通过几行你可以做到这一点 这是文件 https://firebase.google.com/docs/dynamic-links/ 密码是

  Uri dynamicLinkUri = dynamicLink.getUri();
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.google.jo/"))
.setDynamicLinkDomain("rw4r7.app.goo.gl")
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,  shortLink.toString());
intent.setType("text/plain");
startActivity(intent);
} else {
// Error
// ...
}
}
});

也可以从支持库中使用 ShareCompat类。

ShareCompat.IntentBuilder.from(activity)
.setType("text/plain")
.setChooserTitle("Chooser title")
.setText("http://play.google.com/store/apps/details?id=" + activity.getPackageName())
.startChooser();

Https://developer.android.com/reference/android/support/v4/app/sharecompat.html

你可以分享任何你想分享的东西

fun Context.share(text: String) =
this.startActivity(Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, text)
type = "text/plain"
})

用法

context.share("Check https://stackoverflow.com")

根据 官方文件在2021年的首选方式是

fun shareTextToOtherApps(message: String) {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, message)
type = "text/plain"
}


val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
}

@ Linh 的回答几乎是好的,但是由于缺少 FLAG _ ACTIVITY _ NEW _ TASK 而导致系统崩溃,以下是对我有效的方法

    public static void shareApp(Context context) {
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
sendIntent.setType("text/plain");
context.startActivity(sendIntent);
}
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Application name");
String shareMessage= "\n Your Message \n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) {
//e.toString();
}