//MainActivity 类
public class MainActivity extends AppCompatActivity { private Button btn; private TextView tv; private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); image = (ImageView) findViewById(R.id.image); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, umAuthListener); } }); } 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.setText(name + " " +gender); ImageLoader.getInstance().displayImage(photoUrl,image); } @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); } }
Appliction
public class appliction extends Application{ private static appliction 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 appliction 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);//全局初始化此配置 } }项目清单
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bwie.com.myapplication"> <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.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name=".Appliction" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </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="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/> </application> <meta-data android:name="UMENG_APPKEY" android:value="59af450107fe652856001419" > </meta-data> </manifest>buildTypes { release { minifyEnabled false signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { minifyEnabled false signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } signingConfigs { debug { storeFile file('debug.keystore') storePassword "android" keyAlias "androiddebugkey" keyPassword "android" } }