package com.example.lichunsheng; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.umeng.socialize.ShareAction; import com.umeng.socialize.UMAuthListener; import com.umeng.socialize.UMShareAPI; import com.umeng.socialize.UMShareListener; import com.umeng.socialize.bean.SHARE_MEDIA; import com.umeng.socialize.media.UMImage; import java.util.Map; public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private Button fen; private Button san; private UMAuthListener umAuthListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); } private void initView() { fen = (Button) findViewById(R.id.fen); san = (Button) findViewById(R.id.san); fen.setOnClickListener(this); san.setOnClickListener(this); } //5a73c4c9b27b0a78ae0002e7 @Override public void onClick(View v) { UMImage umImage=new UMImage(this,R.mipmap.ic_launcher); switch (v.getId()) { case R.id.fen: new ShareAction(LoginActivity.this) .setPlatform(SHARE_MEDIA.QQ)//传入平台 .withText("hello")//分享内容 .withMedia(umImage) .setCallback(umShareListener)//回调监听器 .share(); break; case R.id.san: initData(); UMShareAPI.get(this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener); break; } } private void initData() { //授权开始的回调 umAuthListener = new UMAuthListener() { @Override public void onStart(SHARE_MEDIA platform) { //授权开始的回调 } @Override public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) { Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show(); } @Override public void onError(SHARE_MEDIA platform, int action, Throwable t) { Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show(); } @Override public void onCancel(SHARE_MEDIA platform, int action) { Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show(); } }; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } private UMShareListener umShareListener = new UMShareListener() { @Override public void onStart(SHARE_MEDIA platform) { } @Override public void onResult(SHARE_MEDIA platform) { Toast.makeText(LoginActivity.this,"成功了",Toast.LENGTH_LONG).show(); } @Override public void onError(SHARE_MEDIA platform, Throwable t) { Toast.makeText(LoginActivity.this,"失败"+t.getMessage(),Toast.LENGTH_LONG).show(); } @Override public void onCancel(SHARE_MEDIA platform) { Toast.makeText(LoginActivity.this,"取消了",Toast.LENGTH_LONG).show(); } }; }
友盟分享和第三方登陆
最新推荐文章于 2018-05-25 19:18:17 发布