Android qq分享与qzone分享

庄康胜
2023-12-01

这是一个固定的用法我觉得,所以我就直接上代码了
当然第一步,导入jar包AndroidManifest.xml的设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rtyui.glide">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".App"
        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>

        <!--<meta-data-->
            <!--android:name="com.example.rtyui.glide.OkHttpGlideModule"-->
            <!--android:value="GlideModule"/>-->

        <!--QQ分享-->
        <activity
            android:name="com.tencent.tauth.AuthActivity"
            android:noHistory="true"
            android:launchMode="singleTask" >
            <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="tencent222222" />
            </intent-filter>
        </activity>

        <activity android:name="com.tencent.connect.common.AssistActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:screenOrientation="portrait" />
    </application>

</manifest>

使用的方式
package com.example.rtyui.glide;

import android.content.Intent;
import android.icu.text.StringSearch;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class MainActivity extends AppCompatActivity {

    private Tencent tencent = null;

    private BaseUiListener baseUiListenerQQ = null;
    private BaseUiListener baseUiListenerQzone = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        baseUiListenerQQ = new BaseUiListener();
        baseUiListenerQzone = new BaseUiListener();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tencent = Tencent.createInstance("222222", this.getApplicationContext());
        findViewById(R.id.share_qq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareQQ();
            }
        });
        findViewById(R.id.share_qzone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareQzone();
            }
        });
    }

    //分享回掉对象
    private class BaseUiListener implements IUiListener {


        protected void doComplete(JSONObject values) {

        }

        @Override
        public void onComplete(Object o) {
            Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override

        public void onError(UiError e) {
            Toast.makeText(MainActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override

        public void onCancel() {
            Toast.makeText(MainActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 分享到QQ
     */
    private void shareQQ(){
        Bundle bundle = new Bundle();
        System.out.println("测试");
        //这条分享消息被好友点击后的跳转URL。
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://connect.qq.com/");
        //分享的标题。注:PARAM_TITLE、PARAM_IMAGE_URL、PARAM_SUMMARY不能全为空,最少必须有一个是有值的。
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "标题");
        //分享的图片URL
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://edu.csdn.net/member?utm_source=login");
        //分享的消息摘要,最长50个字
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "内容");
        //标识该消息的来源应用,值为应用名称+AppId。
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "春泥文化");

        tencent.shareToQQ(this, bundle , baseUiListenerQQ);
    }

    /**
     * 分享到qq空间
     */
    private void shareQzone(){
        Bundle bundle = new Bundle();
        //这条分享消息被好友点击后的跳转URL。
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://connect.qq.com/");
        //分享的标题。注:PARAM_TITLE、PARAM_IMAGE_URL、PARAM_SUMMARY不能全为空,最少必须有一个是有值的。
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "标题");
        //分享的图片URL
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://edu.csdn.net/member?utm_source=login");
        //分享的消息摘要,最长50个字
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "内容");
        //标识该消息的来源应用,值为应用名称+AppId。
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "春泥文化");
        bundle.putInt(QQShare.SHARE_TO_QQ_EXT_INT,1);
        tencent.shareToQQ(this, bundle , baseUiListenerQzone);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.ACTIVITY_OK){
            switch (requestCode) {
                case Constants.REQUEST_QQ_SHARE:
                    Tencent.handleResultData(data, baseUiListenerQQ);
                    break;
                case Constants.REQUEST_QZONE_SHARE:
                    Tencent.handleResultData(data, baseUiListenerQzone);
                    break;
            }
        }
    }
}

 


 类似资料: