1.导入jar包
2.设置权限
3.builde里面设置appkey
4.App里面设置初始化
5.在activity中写入代码
第二步:
<meta-data android:name="UMENG_APPKEY" android:value="4eaee02c527015373b000003"></meta-data>
第三步:
buildTypes { release { minifyEnabled false proguardFiles 'proguard-rules.pro' signingConfig signingConfigs.debug } debug { minifyEnabled false proguardFiles 'proguard-rules.pro' signingConfig signingConfigs.debug } } signingConfigs { debug { storeFile file('debug.keystore') storePassword "android" keyAlias "androiddebugkey" keyPassword "android" } }
第四步:app中
//打Log UMConfigure.setLogEnabled(true); // UMConfigure.init(this, "5a12384aa40fa3551f0001d1" , "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0 //QQ PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba"); //微信 PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
第五步:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 initKong(); //设置权限 if (Build.VERSION.SDK_INT >= 23) { String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS}; ActivityCompat.requestPermissions(this, mPermissionList, 123); } //QQ点击事件 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { authorization(SHARE_MEDIA.QQ); } }); //微信点击事件 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { authorization(SHARE_MEDIA.WEIXIN); } }); //分享 button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { UMImage umImage = new UMImage(MainActivity.this, R.drawable.ic_launcher_background); UMWeb umWeb = new UMWeb("https://www.baidu.com/"); umWeb.setTitle("标题"); umWeb.setThumb(umImage); //分享 new ShareAction(MainActivity.this).withText("111").withMedia(umWeb) .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN) .open(); } }); } private void initKong() { button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); button3 = findViewById(R.id.button3); } private void authorization(SHARE_MEDIA share_media) { UMShareAPI.get(this).getPlatformInfo(this, share_media, new UMAuthListener() { @Override public void onStart(SHARE_MEDIA share_media) { } @Override public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) { Log.d(TAG, "授权完成"); String name = map.get("name"); String gender = map.get("gender"); String iconurl = map.get("iconurl"); Toast.makeText(getApplicationContext(), "昵称:" + name + "性别:" + gender + iconurl, Toast.LENGTH_SHORT).show(); } @Override public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) { Log.d(TAG, "授权错误"); } @Override public void onCancel(SHARE_MEDIA share_media, int i) { Log.d(TAG, "授权取消"); } }); } //分享的监听 private UMShareListener umShareListener = new UMShareListener() { @Override public void onStart(SHARE_MEDIA share_media) { } @Override public void onResult(SHARE_MEDIA share_media) { Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show(); } @Override public void onError(SHARE_MEDIA share_media, Throwable throwable) { Toast.makeText(getApplicationContext(), "错误", Toast.LENGTH_SHORT).show(); } @Override public void onCancel(SHARE_MEDIA share_media) { Toast.makeText(getApplicationContext(), "取消", Toast.LENGTH_SHORT).show(); } }; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } @Override protected void onDestroy() { super.onDestroy(); UMShareAPI.get(this).release(); }