需要添加的权限
<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"/>
//一定别忘了注册Myapp
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,"");
}
}
Mainifest里面添加的
<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"/>
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UMImage image = new UMImage(MainActivity.this, R.drawable.umeng_socialize_qq);
//如果分享网页的话请把下面代码的注释去掉,并且把下方的withMedia(image)中的image改成web
/* UMWeb web = new UMWeb(s);
web.setTitle("hhh");//标题
web.setThumb(image);
web.setDescription("jbfjabndj");//描述*/
new ShareAction(MainActivity.this).withMedia(image) .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();
}
};
}
微信登录一定要新建一个wxapi的包 里面建一个WXEntryActivity的类
public class WXEntryActivity extends WXCallbackActivity {
}