QQ登录和分享(精简版)

使用友盟第三方 http://dev.umeng.com/social/android/quick-integration下载SDK
下载QQ精简版


将下载下来的main文件夹以及platform(选择你想使用的平台即可)文件下,对应的资源文件和jar放入你的工程

添加权限:

android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.ACCESS_WIFI_STATE" />
android:name="android.permission.READ_PHONE_STATE" />
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:name="android.permission.INTERNET" />
android:name="android.permission.ACCESS_FINE_LOCATION" />
android:name="android.permission.ACCESS_COARSE_LOCATION" />
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

使用platform/QQ精简版下的jar和res即可 精简版需要在AndroidManifest中配置的如下:

放在Activity下

        android:name="com.umeng.qq.tencent.AuthActivity"
        android:launchMode="singleTask"
        android:noHistory="true" >

        
            android:name="android.intent.action.VIEW" />
            android:name="android.intent.category.DEFAULT" />
            android:name="android.intent.category.BROWSABLE" />
            android:scheme="tencent100424468" />
        
    

            android:name="com.umeng.qq.tencent.AssistActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:configChanges="orientation|keyboardHidden|screenSize"/>

    android:name="UMENG_APPKEY"
    android:value="58c63564bbea8335dc001770" >

修改build.gradle文件

添加:

signingConfigs {
    debug {
        storeFile file('debug.keystore')
        storePassword "android"
        keyAlias "androiddebugkey"
        keyPassword "android"
    }
}

写一个MyApp的类

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        UMShareAPI.get(this);
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(getApplicationContext()).memoryCacheExtraOptions(480,800).build();
        ImageLoader.getInstance().init(configuration);
    }
    {
        PlatformConfig.setQQZone("1106036236", "mjFCi0oxXZKZEWJs");
    }
}
AndroidManifest.xml文件里的Activity中配置

android:name="app.MyApp"

MainActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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 com.umeng.socialize.shareboard.SnsPlatform;

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button button;
    public ArrayList platforms = new ArrayList();
    private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ};
    private Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.but);
        button1 = (Button) findViewById(R.id.but_fenxiang);
        initPlatforms();
        //QQ好友
        final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform);
        //点击分享
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //四个分享 查看链接  http://dev.umeng.com/social/android/share-detail#4_1
                UMImage image = new UMImage(MainActivity.this, "https://timgsa.baidu.com/timg?image&quality" +
                        "=80&size=b9999_10000&sec=1489994233&di=c5b3c72b34e883b2826c390bb7ad6c81&imgtype=jpg&e" +
                        "r=1&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fit" +
                        "em%2F63d9f2d3572c11df28e42e30602762d0f703c2e8.jpg");//网络图片
                //分享QQ好友(QQ空间  SHARE_MEDIA.QZONE)
                new ShareAction(MainActivity.this).setPlatform(SHARE_MEDIA.QQ)
                        .withText("hello").withMedia(image)
                        .setCallback(umShareListener)
                        .share();
            }
        });
        //点击登录
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isauth) {
                    //授权登录
                    UMShareAPI.get(MainActivity.this).deleteOauth(MainActivity.this, platforms.get(0).mPlatform, authListener);
                } else {
                    //点击登录
//                    UMShareAPI.get(MainActivity.this).doOauthVerify(MainActivity.this, platforms.get(0).mPlatform, authListener);
                    //获取信息
                    UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, platforms.get(0).mPlatform, authListener);
                }
            }
        });

    }

    private void initPlatforms() {
        for (SHARE_MEDIA e : list) {
            if (!e.toString().equals(SHARE_MEDIA.GENERIC.toString())) {
                platforms.add(e.toSnsPlatform());
            }
        }
    }

    UMAuthListener authListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //SocializeUtils.safeShowDialog(dialog);
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map, String> data) {
            Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
            String profile_image_url = data.get("profile_image_url");
            String name = data.get("name");
            Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
            Toast.makeText(MainActivity.this, profile_image_url, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(MainActivity.this, "失败:", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };


    //分享方法
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
        }
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat","platform"+platform);

            Toast.makeText(MainActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(MainActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
    //此方法只能放在Activity里 fragment可以放在依赖的Activity里
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}


你可能感兴趣的:(QQ登录和分享(精简版))