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

时间:2013-06-18 11:27:29

标签: java android xml button share

我想在我的Android应用中添加“分享”按钮。

喜欢那个

:

我添加了“分享”按钮,但按钮未激活。我点击了,但没有任何反应。

我在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)中分享文字。

选项卡(xml)中的代码(如果需要):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >

<TextView
    android:id="@+id/section_label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/text"
    android:textColor="@color/text_color" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sprite" />

4 个答案:

答案 0 :(得分:273)

添加Button,然后点击Button添加此代码:

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"));

有用的链接:

For basic sharing

For customization

答案 1 :(得分:8)

创建一个包含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"));
    }
});

以上代码段将在共享按钮点击操作上打开共享选择器。 但请注意......共享代码段可能无法使用模拟器输出非常好的结果。对于实际结果,请在Android设备上运行代码段以获得真实结果。

答案 2 :(得分:1)

在科特林:

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 :"))

答案 3 :(得分:1)

共享以下任何文件(Kotlin):
首先在 flutter doctor -v 文件夹中创建一个名为 [✓] Flutter (Channel stable, 1.20.3, on Mac OS X 10.15.6 19G2021, locale en-GB) • Flutter version 1.20.3 at /usr/local/Cellar/flutter/sdk • Framework revision 216dee60c0 (6 days ago), 2020-09-01 12:24:47 -0700 • Engine revision d1bc06f032 • Dart version 2.9.2 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/user/Library/Android/sdk • Platform android-30, build-tools 30.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 11.7) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.7, Build version 11E801a • CocoaPods version 1.9.3 [!] Android Studio (version 4.0) • Android Studio at /Applications/Android Studio.app/Contents ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] VS Code (version 1.48.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.14.0 [✓] Connected device (1 available) • iPhone 11 (mobile) • 034B59B3-8929-4997-B7A1-53E32D065347 • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-7 (simulator) ! Doctor found issues in 1 category. 的文件夹,并创建一个名为 xml的新XML资源文件。 em>并将以下代码放入其中:

res

现在转到 provider_paths.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>