java 如何在我的 android 应用程序上添加共享按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37734887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a share button on my android app
提问by Hessel
I want to add a share button on my app.
我想在我的应用程序上添加一个共享按钮。
If users use the app there would be an option that they can share my app, but unfortunately it's not happening.
如果用户使用该应用程序,他们可以选择共享我的应用程序,但不幸的是它没有发生。
Here is my code:
这是我的代码:
MainActivity.java
主活动.java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url ="http://dehvb.nl/";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.loadUrl(url);
}
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "here goes your share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
//then set the sharingIntent
mShareActionProvider.setShareIntent(sharingIntent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_item_share) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml
菜单_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity">
<item
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="Share"
android:icon="@drawable/share"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
LogCat:
日志猫:
06-10 14:37:09.841 24733-24733/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ed7b38d time:33915488
06-10 14:37:15.501 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_Ime: ImeThread is not enabled.
06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/Activity: performCreate Call Injection manager
06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOnViewCreated > Target : com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity isFragment :false
06-10 14:37:15.521 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdccbf864
06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdd13fc54
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: Initialized EGL, version 1.4
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: HWUI protection enabled for context , &this =0xeedbea00 ,&mEglDisplay = 1 , &mEglConfig = -189468004
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Enabling debug mode 0
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchCreateOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchPrepareOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-10 14:37:15.901 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ed7b38d time:33921540
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28065
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
06-10 14:37:21.811 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
06-10 14:37:21.881 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-10 14:37:22.261 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
06-10 14:37:22.361 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/AndroidRuntime: Shutting down VM
06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vvhvb.hesselfeenstra.vvheerenveenseboys, PID: 31316
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ShareActionProvider.setShareIntent(android.content.Intent)' on a null object reference
at com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity.onCreateOptionsMenu(MainActivity.java:48)
at android.app.Activity.onCreatePanelMenu(Activity.java:3124)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
06-10 14:40:54.201 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Process: Sending signal. PID: 31316 SIG: 9
回答by ishmaelMakitla
Firstly, modify your menu.xml file to include the share-action menu item:
首先,修改您的 menu.xml 文件以包含共享操作菜单项:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_share"
android:icon="@drawable/share"
app:showAsAction="ifRoom" />
<-- here is the share action menu item -->
<item
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="Share"
app:actionProviderClass=
"android.support.v7.widget.ShareActionProvider" />
</menu>
Then change your onCreateOptionsMenu
by adding the ShareActionProvider
code based on the share-action menu item:
然后onCreateOptionsMenu
通过添加ShareActionProvider
基于共享操作菜单项的代码来更改您的:
//add the following imports (you may already have them)
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
...
private ShareActionProvider mShareActionProvider;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
//create the sharing intent
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "here goes your share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
//then set the sharingIntent
mShareActionProvider.setShareIntent(sharingIntent);
return true;
}
I hope this helps you. Please give it a try and let me know. You can also consult these closely related links:
我希望这可以帮助你。请试一试,让我知道。您还可以查阅这些密切相关的链接:
Activating Share Button in Android