在安卓应用程序中如何激活“分享”按钮?

我想添加“分享”按钮到我的安卓应用程序。

就像这样

:

我添加了“分享”按钮,但是没有激活。我点击,但是什么也没有发生。

我在 MainActivity.java 中的代码:

private ShareActionProvider mShareActionProvider;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.share_menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());


return true;
}


{
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

我希望在第一个选项卡(first _ tab.xml)或第二个选项卡(second _ tab.xml)中共享文本。

Code in tab (xml) (If need):








177112 次浏览

Add a Button and on click of the Button add this code:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

有用连结:

基本分享

用于定制

创建一个具有 id 共享的按钮,并添加以下代码段。

share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Your body here";
String shareSub = "Your subject here";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
});

上面的代码片段将在共享按钮单击操作时打开共享选择器。 However, note...The share code snippet might not output very good results using emulator. For actual results, run the code snippet on android device to get the real results.

在 Kotlin:

val sharingIntent = Intent(android.content.Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
val shareBody = "Application Link : https://play.google.com/store/apps/details?id=${App.context.getPackageName()}"
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "App link")
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody)
startActivity(Intent.createChooser(sharingIntent, "Share App Link Via :"))

分享以下任何文件(Kotlin) :
首先在 res文件夹中创建一个名为 xml的文件夹,然后创建一个名为 provider_paths.xml的新 XML 资源文件,并将以下代码放入其中:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="files"
path="."/>


<external-path
name="external_files"
path="."/>
</paths>

现在转到 manifests文件夹,打开 AndroidManifest.xml,然后把下面的代码放在 <application>标签中:

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" /> // provider_paths.xml file path in this example
</provider>

现在你把下面的代码放入 setOnLongClickListener:

share_btn.setOnClickListener {
try {
val file = File("pathOfFile")
if(file.exists()) {
val uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file)
val intent = Intent(Intent.ACTION_SEND)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setType("*/*")
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent)
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
toast("Error")
}
}

Kotlin

在单击监听器内部,需要添加这个模块,以便通过 whatsApp、 email、 Slack 等应用程序共享文本。

shareOptionClicked.setOnClickListener{
val shareData = Intent(Intent.ACTION_SEND)
shareData.type = "text/plain"
val dataToShare = "Text from my application"
shareData.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
shareData.putExtra(Intent.EXTRA_TEXT, dataToShare)
startActivity(Intent.createChooser(shareData, "Share Via"))
}