Skip to content

Instantly share code, notes, and snippets.

@lvsecoto
Created February 12, 2019 02:14
Show Gist options
  • Save lvsecoto/9c9d66f7ee033179a4688f7c1783c944 to your computer and use it in GitHub Desktop.
Save lvsecoto/9c9d66f7ee033179a4688f7c1783c944 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import com.annimon.stream.Stream;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.util.Map;
/** 通过这个Activity可以方便获取微信用户信息 */
public class WeChatLoginActivity extends Activity {
public static void start(Activity activity, int requestCode) {
Intent intent = createIntent(activity);
activity.startActivityForResult(intent, requestCode);
}
public static void start(Fragment fragment, int requestCode) {
Intent intent = createIntent(fragment.getContext());
fragment.startActivityForResult(intent, requestCode);
}
private static Intent createIntent(Context context) {
Intent intent = new Intent(context, WeChatLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return intent;
}
/**
* 获取用户名
*
* @param data {@link #onActivityResult(int, int, Intent)}回调的参数
*/
public static String getUserName(Intent data) {
return data.getStringExtra("name");
}
/**
* 获取用户头像
*
* @param data {@link #onActivityResult(int, int, Intent)}回调的参数
*/
public static String getUserAvatar(Intent data) {
return data.getStringExtra("iconurl");
}
/**
* 获取用户OpenId
*
* @param data {@link #onActivityResult(int, int, Intent)}回调的参数
*/
public static String getUserOpenId(Intent data) {
return data.getStringExtra("openid");
}
/**
* 获取用户UnionId
*
* @param data {@link #onActivityResult(int, int, Intent)}回调的参数
*/
public static String getUserUnionId(Intent data) {
return data.getStringExtra("unionid");
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupLoadingLayout();
hideStatusBar();
clearAndGetUserInfo();
}
private void setupLoadingLayout() {
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER));
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(progressBar);
setContentView(frameLayout);
}
private void hideStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
private void clearAndGetUserInfo() {
UMShareAPI.get(this)
.deleteOauth(
this,
SHARE_MEDIA.WEIXIN,
new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {}
@Override
public void onComplete(
SHARE_MEDIA share_media, int i, Map<String, String> map) {
getUserInfo();
}
@Override
public void onError(
SHARE_MEDIA share_media, int i, Throwable throwable) {
setResult(Activity.RESULT_CANCELED);
finish();
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
setResult(Activity.RESULT_CANCELED);
finish();
}
});
}
private void getUserInfo() {
UMShareAPI.get(this)
.getPlatformInfo(
this,
SHARE_MEDIA.WEIXIN,
new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {}
@Override
public void onComplete(
SHARE_MEDIA share_media, int i, Map<String, String> map) {
Intent intent = new Intent();
Stream.of(map)
.forEach(
entry ->
intent.putExtra(
entry.getKey(), entry.getValue()));
setResult(Activity.RESULT_OK, intent);
finish();
}
@Override
public void onError(
SHARE_MEDIA share_media, int i, Throwable throwable) {
setResult(Activity.RESULT_CANCELED);
finish();
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
setResult(Activity.RESULT_CANCELED);
finish();
}
});
}
}
PlatformConfig.setWeixin(WX_APP_ID, WX_KEY);
<activity
android:name=".wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
import com.umeng.socialize.weixin.view.WXCallbackActivity;
public class WXEntryActivity extends WXCallbackActivity {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment