关闭

第三方登录QQweb版

标签: android
64人阅读 评论(0) 收藏 举报
分类:

一.导入jar包

二.导入清单文件中的activity和权限

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


 <activity
            android:name="com.tencent.tauth.TAuthView"
            android:configChanges="keyboardHidden|navigation|orientation"
            android:launchMode="singleTask"
            >
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <!-- <data android:scheme="auth"/> -->

            </intent-filter>
        </activity>

三核心代码

   Intent intent = new Intent(MainActivity.this, com.tencent.tauth.TAuthView.class);
        //app的id
        intent.putExtra(TAuthView.CLIENT_ID, "222222");
        //获取qq登录信息的范围
        intent.putExtra(TAuthView.SCOPE, "all");
        //利用自带浏览器
        intent.putExtra(TAuthView.TARGET, "_self");
        //		intent.putExtra(TAuthView.CALLBACK, CALLBACK);

        startActivity(intent);


四完整代码

1.布局文件

LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.loginfromwebqq.MainActivity">

    <Button
        android:onClick="login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"/>
    <TextView
        android:id="@+id/text"
        android:text="none"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

2.清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.administrator.loginfromwebqq">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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.TAuthView"
            android:configChanges="keyboardHidden|navigation|orientation"
            android:launchMode="singleTask"
            >
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <!-- <data android:scheme="auth"/> -->

            </intent-filter>
        </activity>
    </application>

</manifest>

3.java代码

public class MainActivity extends AppCompatActivity {

    private AuthReceiver receiver;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.text);
        //注册广播
        registerIntentReceivers();
    }
    //点击跳转到web版qq登录
    public  void login(View view){
        Intent intent = new Intent(MainActivity.this, com.tencent.tauth.TAuthView.class);
        //app的id
        intent.putExtra(TAuthView.CLIENT_ID, "222222");
        //获取qq登录信息的范围
        intent.putExtra(TAuthView.SCOPE, "all");
        //利用自带浏览器
        intent.putExtra(TAuthView.TARGET, "_self");
        //		intent.putExtra(TAuthView.CALLBACK, CALLBACK);

        startActivity(intent);
    }

    //注册一个广播接受登录以后的信息.
    private void registerIntentReceivers() {
        receiver = new AuthReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(TAuthView.AUTH_BROADCAST);
        registerReceiver(receiver, filter);
    }

    public class AuthReceiver extends BroadcastReceiver {

        private static final String TAG = "AuthReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle exts = intent.getExtras();
            String raw = exts.getString("raw");
            String access_token = exts.getString(TAuthView.ACCESS_TOKEN);
            String expires_in = exts.getString(TAuthView.EXPIRES_IN);
            String error_ret = exts.getString(TAuthView.ERROR_RET);
            String error_des = exts.getString(TAuthView.ERROR_DES);
            //拿到accessToken表明登录已经成功
            if (access_token!=null) {
                mTextView.setText(access_token);
            }
            Log.e("result", "token="+access_token);
        }

    }

    private void unregisterIntentReceivers() {
        unregisterReceiver(receiver);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterIntentReceivers();
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8899次
    • 积分:628
    • 等级:
    • 排名:千里之外
    • 原创:51篇
    • 转载:31篇
    • 译文:1篇
    • 评论:0条