package com.example.qqlogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_photo;
private TextView tv_name;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
tv_name = (TextView) findViewById(R.id.tv_name);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
//调用登录api
UMShareAPI.get(this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, umAuthListener);
break;
}
}
private UMAuthListener 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();
String name = data.get("name");
String gender = data.get("gender");
String photoUrl = data.get("iconurl");
tv_name.setText(name + " " +gender);
ImageLoader.getInstance().displayImage(photoUrl,iv_photo);
}
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
if( UMShareAPI.get(MainActivity.this).isInstall(MainActivity.this,SHARE_MEDIA.QQ)){
Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "no install QQ", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
}
};
//第一步倒jar包 ,第二 添加onActivityResult,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
package com.example.qqlogin.app;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import java.io.File;
/**
* Created by hasee on 2017/8/2.
*/
public class AppApplication extends Application {
private static AppApplication mAppApplication;
//第三 加权限 第四功能清单加activity 第五功能清单加meta 第六加下面这行代码
{
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
initImageLoader(getApplicationContext());
mAppApplication = this;
//第七 初始化 ,第八加签名文件 ,第九 在gradle里面配置签名文件
UMShareAPI.get(this);
}
/**
* 获取Application
*/
public static AppApplication getApp() {
return mAppApplication;
}
/** 初始化ImageLoader */
/**
* 初始化ImageLoader
*/
public static void initImageLoader(Context context) {
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "topnews/Cache");//获取到缓存的目录地址
Log.d("cacheDir", cacheDir.getPath());
//创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(context)
.threadPoolSize(3)//线程池内加载的数量
.denyCacheImageMultipleSizesInMemory()//.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024)
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCache(new UnlimitedDiskCache(cacheDir))
//自定义缓存路径//.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
.build();
ImageLoader.getInstance().init(config);//全局初始化此配置
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在Androidmanifest.xml配置文件里添加
<activity
android:name="com.umeng.qq.tencent.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="tencent100424468" />
</intent-filter>
</activity>
<activity
android:name="com.umeng.qq.tencent.AssistActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"/>
<meta-data
android:name="UMENG_APPKEY"
android:value="561cae6ae0f55abd990035bf" >
</meta-data>
------------------------------------------------------------------------------------------------------------------------------
添加imageLoader依赖
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'