实现分享图文消息到QQ

由于最近比较空闲,特别记录下实现分享图文消息到QQ的过程:
开发环境:

  • Android Studio 2.1
  • Android SDK_V2.9.4
  • 到腾讯开放平台注册开发者。http://open.qq.com/
  • 下载Android SDK包,并将其放入项目中
  • AndroidManifest文件配置– 添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  • AndroidManifest文件配置– 注册AuthActivity
<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"/>
                <!--我的App ID,要保留tencent-->
                <data android:scheme="tencent******"/>
            </intent-filter>
</activity>
  • AndroidManifest文件配置–注册AssistActivity
<activity  android:name="com.tencent.connect.common.AssistActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
  • 创建实例
//实例化Tencent类 mTencent = Tencent.createInstance(APP_ID, getApplicationContext());
  • 实现回调接口
private class BaseUIListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
            Log.d(TAG, "分享成功\n" +
                    o.toString());
        }

        @Override
        public void onError(UiError uiError) {
            Log.d(TAG, "分享失败\n" +
                    uiError.toString());
        }

        @Override
        public void onCancel() {
            Log.d(TAG, "分享取消");
        }
    }
  • 实现分享方法
 /** * 分享到QQ */
private void share() {
        mListener = new BaseUIListener();
        final Bundle bundle = new Bundle();
        //图文分享
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        //跳转URL
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "跳转URL");
        //分享的标题
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "Who are you?");
        //分享的消息摘要
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "Hi,我在测试QQ分享接口");
        mTencent.shareToQQ(this, bundle, mListener);
}
  • 调用回调接口
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mListener = new BaseUIListener();
        Tencent.onActivityResultData(requestCode, resultCode, data, mListener);
}

你可能感兴趣的:(android)