例:
![](https://i-blog.csdnimg.cn/blog_migrate/69a748df2d54710bef19fb50df4b2ee5.png)
点击打开链接:https://download.csdn.net/my
将文件中的jar包导入工程中的libs文件夹 并引用,在将
\第三方分享jar包和附件\Android\main\res直接复制到项目中的src文件夹下的main文件夹里,
它会直接补齐你工程中缺少的部分,所以不用害怕它会替换掉你的原文件,
还有以下三个文件夹需要引用:
\第三方分享jar包和附件\Android\QQ精简版\res
\第三方分享jar包和附件\Android\微信精简版\res
\第三方分享jar包和附件\Android\新浪精简版\res
加权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
加依赖jar包:
MainActivity主方法类:
public class MainActivity extends AppCompatActivity { private Button btn; //分享的网页 String s="https://www.baidu.com/?tn=62095104_2_oem_dg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn01); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //分享的图片 UMImage image = new UMImage(MainActivity.this, R.drawable.ic_launcher_background); //如果分享网页的话请把下面代码的注释去掉,并且把下方的withMedia(image)中的image改成web UMWeb web = new UMWeb(s); web.setTitle("hhh");//标题 web.setThumb(image); web.setDescription("jbfjabndj");//描述 //.withMedia(web)把web改成image,再把上面这4行注释掉,就变成分享图片了 new ShareAction(MainActivity.this).withMedia(web) .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN) .setCallback(shareListener).open(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } private UMShareListener shareListener = new UMShareListener() { /** * @descrption 分享开始的回调 * @param platform 平台类型 */ @Override public void onStart(SHARE_MEDIA platform) { } /** * @descrption 分享成功的回调 * @param platform 平台类型 */ @Override public void onResult(SHARE_MEDIA platform) { Toast.makeText(MainActivity.this,"成功 了",Toast.LENGTH_LONG).show(); } /** * @descrption 分享失败的回调 * @param platform 平台类型 * @param t 错误原因 */ @Override public void onError(SHARE_MEDIA platform, Throwable t) { Toast.makeText(MainActivity.this,"失 败"+t.getMessage(),Toast.LENGTH_LONG).show(); } /** * @descrption 分享取消的回调 * @param platform 平台类型 */ @Override public void onCancel(SHARE_MEDIA platform) { Toast.makeText(MainActivity.this,"取消 了",Toast.LENGTH_LONG).show(); } }; }
activity_main布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.day_0524_umeng.MainActivity"> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:text="分享" /> </RelativeLayout>
MyApp 类(记得在清单文件中添加name)
public class MyApp extends Application { { //PlatformConfig.setWeixin("1105602574", "3baf1193c85774b3fd9d18447d76cab0"); PlatformConfig.setQQZone("1106788439", "qwQLs9n3pNWxqFM4"); } @Override public void onCreate() { super.onCreate(); UMConfigure.init(this,"5b01634fa40fa361f20000bb" ,"umeng",UMConfigure.DEVICE_TYPE_PHONE,""); } }
WXEntryActivity 微信分享页面(比价麻烦,我没写,就建个空类就行)
public class WXEntryActivity extends WXCallbackActivity { }
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day_0524_umeng"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".WXApi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar" ></activity> <activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencent1105602574" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/> </application> </manifest>