腾讯QQAndroid API调用实例(QQ分享无需登录)

腾讯QQAndroid API调用实例(QQ分享无需登录)

 

要分为两只步骤:

  1. 配置Androidmanifest.xml

  2. 修改activity里边代码

切切实实修改如下:

 

1、Activity代码

  1. package
    com.corele.qqshare;

  2.  

  3. import
    android.app.Dialog;

  4. import
    android.content.Intent;

  5. import
    android.os.Handler;

  6. import
    android.support.v7.app.AppCompatActivity;

  7. import
    android.os.Bundle;

  8. import
    android.view.View;

  9. import
    android.widget.Button;

  10. import
    android.widget.TextView;

  11.  

  12. import
    com.tencent.connect.share.QQShare;

  13. import
    com.tencent.tauth.IUiListener;

  14. import
    com.tencent.tauth.Tencent;

  15. import
    com.tencent.tauth.UiError;

  16.  

  17. public
    class MainActivity extends AppCompatActivity implements View.OnClickListener{

  18.     private TextView mMessageText;

  19.     private TextView mBaseMessageText;

  20.     private Dialog mProgressDialog;

  21.     private Handler mHandler;

  22.     private Tencent mTencent;

  23.     private Button qqshare;

  24.     @Override

  25.     protected void onCreate(Bundle savedInstanceState) {

  1.         super.onCreate(savedInstanceState);
  1.         setContentView(R.layout.activity_main);
  1.         mTencent=Tencent.createInstance(“1105968442”,this.getApplicationContext());

  2.         initViews();

  3.     }

  4.  

  5.     private void showResult(final String base, final String msg) {

  6.         mHandler.post(new Runnable() {

  7.  

  8.             @Override

  9.             public void run() {

  10.                 if (mProgressDialog.isShowing())

  11.                     mProgressDialog.dismiss();

  1.                 mBaseMessageText.setText(base);
  1.                 mMessageText.setText(msg);
  1.             }

  2.         });

  3.     }

  4.  

  5.     @Override

  6.     public void onClick(View v) {

  7.         onClickShare();

  8.     }

  9.  

  10.     private class BaseUiListener implements IUiListener {

  11.  

  12.         public void onComplete(Object response) {

  13.             mBaseMessageText.setText(“onComplete:”);

  14.             mMessageText.setText(response.toString());

  1.             doComplete(response);
  1.         }

  2.         protected void doComplete(Object values) {

  3.         }

  4.  

  5.         @Override

  6.         public void onError(UiError e) {

  7.             showResult(“onError:”, “code:” + e.errorCode + “, msg:”

  8.                     + e.errorMessage +
    “, detail:” + e.errorDetail);

  1.         }

  2.         @Override

  3.         public void onCancel() {

  4.             showResult(“onCancel”, “”);

  5.         }

  6.     }

  7.  

  8.     private void initViews() {

  9.         mBaseMessageText = (TextView)
    findViewById(R.id.base_message_tv);

  10.         mMessageText = (TextView)
    findViewById(R.id.message_tv);

  11.         qqshare=(Button)
    findViewById(R.id.qq_share);

  12.  

  13.         qqshare.setOnClickListener(this);

  14.     }

  15.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  16.         if (null
    != mTencent)

  17.             mTencent.onActivityResult(requestCode,
    resultCode, data);

  18.     }

  19.  

  20.     private void onClickShare() {

  21.         final Bundle params = new Bundle();

  22.         params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
    QQShare.SHARE_TO_QQ_TYPE_DEFAULT);

  23.         params.putString(QQShare.SHARE_TO_QQ_TITLE,
    “乐计算”);

  24.         params.putString(QQShare.SHARE_TO_QQ_SUMMARY,
    “一磨蹭简单的乘除工具”);

  25.         params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,
    “http://a.app.qq.com/o/simple.jsp?pkgname=com.corel.calc”);

  1.         params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,”http://imgcache.qq.com/qzone/space\_item/pre/0/66768.gif”);
  1.         params.putString(QQShare.SHARE_TO_QQ_APP_NAME,
    “乐计算”);

  2.         mTencent.shareToQQ(MainActivity.this, params, new BaseUiListener());

  3.     }

  4. }

 

 

 

2、Androidmanifest.xml代码

内主要增长代码

ACCESS 1

  1. <?xml
    version=”1.0″ encoding=”utf-8″?>
  1. <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
  1.     package=”com.corele.qqshare”>

  2.     <uses-permission android:name=”android.permission.INTERNET”
    />

  3.     <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”
    />

  4.     <application

  5.         android:allowBackup=”true”

  6.         android:icon=”@mipmap/ic_launcher”

  7.         android:label=”@string/app_name”

  8.         android:supportsRtl=”true”

  9.         android:theme=”@style/AppTheme”>

  10.         <activity
    android:name=”.MainActivity”>

  11.             <intent-filter>

  1.                 <action
    android:name=”android.intent.action.MAIN”
    />

  2.                 <category
    android:name=”android.intent.category.LAUNCHER”
    />

  3.             </intent-filter>

  4.         </activity>

  5.         <activity

  1.             android:name=”com.tencent.tauth.AuthActivity”

  2.             android:noHistory=”true”

  3.             android:launchMode=”singleTask” >

  4.             <intent-filter>

  1.                 <action
    android:name=”android.intent.action.VIEW”
    />

  2.                 <category
    android:name=”android.intent.category.DEFAULT”
    />

  3.                 <category
    android:name=”android.intent.category.BROWSABLE”
    />

  4.                 <data android:scheme=”tencent1105968442″ />

  5.             </intent-filter>

  6.         </activity>

  7.         <activity
    android:name=”com.tencent.connect.common.AssistActivity”

  1.             android:theme=”@android:style/Theme.Translucent.NoTitleBar”
  1.             android:configChanges=”orientation|keyboardHidden|screenSize”
  1.             />

  2.     </application>

  1.  

  2. </manifest>

33、效果亮

ACCESS 2ACCESS 3

ACCESS 4

相关文章