package com.whr.demo.mytwitter.mytwitter;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import com.twitter.sdk.android.core.models.User;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;
import java.net.MalformedURLException;
import java.net.URL;
import retrofit2.Call;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnShareByTwitter, btnLogin;
TwitterLoginButton twitterLoginButton;
private final int TWEET_COMPOSER_REQUEST_CODE = 1;
TwitterLoginUtils twitterLoginUtils;
Uri uri;
private TextView tvShowUserMessage;
private String TAG = "--->whr";
private boolean isMySelfButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvShowUserMessage = findViewById(R.id.tv_user_message);
btnShareByTwitter = findViewById(R.id.btn_share_twitter);
btnShareByTwitter.setOnClickListener(this);
btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
//官方按钮登陆
twitterLoginButton = findViewById(R.id.btn_login_by_twitter);
loginByTwitterButton();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_share_twitter:
shareToTwitter();
break;
case R.id.btn_login:
loginByMyButton();
break;
default:
break;
}
}
/**
* 官方按钮登陆Twitter
*/
private void loginByTwitterButton() {
Log.e(TAG, "第一步");
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
isMySelfButton = false;
Log.e(TAG, "第二步");
TwitterSession session = result.data;
final String userName = session.getUserName();
Call<User> user = TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, true, false);
user.enqueue(new Callback<User>() {
@Override
public void success(Result<User> result) {
Log.e(TAG, "第3步");
User userInfo = result.data;
String email = userInfo.email;
String description = userInfo.description;
Log.e(TAG, "------------" + userInfo.profileImageUrl);
StringBuilder sb = new StringBuilder();
sb.append("User Name: " + userName);
sb.append("\n");
sb.append("Email: " + email);
sb.append("\n");
sb.append("Description: " + description);
sb.append("\n");
tvShowUserMessage.setText(sb.toString());
Log.e(TAG, "sb.toString():" + sb.toString());
}
@Override
public void failure(TwitterException exception) {
Log.e(TAG, "第4步");
}
});
}
@Override
public void failure(TwitterException exception) {
Log.e(TAG, "第5步");
Log.e("TwitterKit", "Login with Twitter failure", exception);
Log.e("BLA: ", exception.getMessage());
}
});
}
/**
* 自定义按钮登陆Twitter
*/
private void loginByMyButton() {
isMySelfButton = true;
twitterLoginUtils = new TwitterLoginUtils(this);
twitterLoginUtils.setOnLoginByTwitterClick(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Log.e(TAG, "第二步");
TwitterSession session = result.data;
final String userName = session.getUserName();
Call<User> user = TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, true, false);
user.enqueue(new Callback<User>() {
@Override
public void success(Result<User> result) {
Log.e(TAG, "第3步");
User userInfo = result.data;
String email = userInfo.email;
String description = userInfo.description;
Log.e(TAG, "------------ " + userInfo.profileImageUrl);
StringBuilder sb = new StringBuilder();
sb.append("User Name: " + userName);
sb.append("\n");
sb.append("Email: " + email);
sb.append("\n");
sb.append("Description: " + description);
sb.append("\n");
tvShowUserMessage.setText(sb.toString());
Log.e(TAG, "sb.toString():" + sb.toString());
}
@Override
public void failure(TwitterException exception) {
Log.e(TAG, "第4步");
}
});
}
@Override
public void failure(TwitterException exception) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE) {
Log.e(TAG, "第6步");
if (isMySelfButton) {
twitterLoginUtils.onActivityResult(requestCode, resultCode, data);
} else {
twitterLoginButton.onActivityResult(requestCode, resultCode, data);
}
}
}
/******************************以下为推特分享********************************/
/**
* 分享到Twitter
* 若未安装twitter客户端,则会跳转到浏览器
*
* @throws MalformedURLException
*/
private void shareToTwitter() {
uri = resourceIdToUri(R.mipmap.bg);
Log.e("--->whr", "--->Uri:" + uri);
Intent intent = null;
try {
intent = new TweetComposer.Builder(this)
.text("百度链接:")
.url(new URL("https://www.baidu.com/"))
.image(uri)
.createIntent();
} catch (MalformedURLException e) {
e.printStackTrace();
}
startActivityForResult(intent, TWEET_COMPOSER_REQUEST_CODE);
}
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";
private Uri resourceIdToUri(int resourceId) {
return Uri.parse(ANDROID_RESOURCE + getPackageName() + FOREWARD_SLASH + resourceId);
}
/******************************以上为推特分享********************************/
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MyTwitter.zip (76个子文件)
MyTwitter
.gitignore 176B
app
.gitignore 7B
build.gradle 1018B
src
main
AndroidManifest.xml 824B
res
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
layout
activity_main.xml 1KB
mipmap-xxhdpi
ic_launcher_round.png 10KB
bg.jpeg 73KB
ic_launcher.png 6KB
drawable
ic_launcher_background.xml 5KB
mipmap-anydpi-v26
ic_launcher_round.xml 272B
ic_launcher.xml 272B
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
values
strings.xml 251B
colors.xml 208B
styles.xml 383B
java
com
whr
demo
mytwitter
mytwitter
MainActivity.java 8KB
TwitterLoginUtils.java 3KB
MyApp.java 885B
test
java
com
whr
demo
mytwitter
mytwitter
ExampleUnitTest.java 393B
.DS_Store 6KB
libs
proguard-rules.pro 751B
app.iml 12KB
settings.gradle 15B
build.gradle 546B
gradlew 5KB
gradlew.bat 2KB
gradle.properties 727B
.gradle
4.6
javaCompile
taskJars.bin 43KB
taskHistory.bin 85KB
classAnalysis.bin 1.58MB
jarAnalysis.bin 663KB
javaCompile.lock 17B
fileContent
fileContent.lock 17B
fileHashes
fileHashes.bin 405KB
fileHashes.lock 17B
resourceHashesCache.bin 21KB
taskHistory
taskHistory.bin 1.05MB
taskHistory.lock 17B
fileChanges
last-build.bin 1B
buildOutputCleanup
outputFiles.bin 21KB
buildOutputCleanup.lock 17B
cache.properties 49B
vcsWorkingDirs
gc.properties 0B
.idea
misc.xml 2KB
libraries
Gradle__com_twitter_sdk_android_twitter_core_3_1_1.xml 682B
Gradle__com_twitter_twitter_text_1_14_3_jar.xml 548B
Gradle__com_twitter_sdk_android_tweet_ui_3_1_1.xml 662B
Gradle__com_android_support_appcompat_v7_23_1_1.xml 1014B
Gradle__com_android_support_support_annotations_23_1_1_jar.xml 739B
Gradle__com_android_support_support_v4_23_1_1.xml 1KB
Gradle__com_squareup_picasso_picasso_2_5_2_jar.xml 545B
Gradle__com_twitter_sdk_android_tweet_composer_3_1_1.xml 692B
Gradle__com_squareup_okhttp3_okhttp_3_4_2_jar.xml 540B
Gradle__com_android_support_recyclerview_v7_23_1_1.xml 1KB
Gradle__com_squareup_okio_okio_1_9_0_jar.xml 521B
Gradle__com_squareup_retrofit2_converter_gson_2_1_0_jar.xml 586B
Gradle__com_squareup_retrofit2_retrofit_2_1_0_jar.xml 556B
Gradle__com_google_code_gson_gson_2_7_jar.xml 520B
caches
build_file_checksums.ser 543B
workspace.xml 205KB
gradle.xml 626B
codeStyles
Project.xml 2KB
runConfigurations.xml 564B
modules.xml 355B
gradle
wrapper
gradle-wrapper.properties 200B
gradle-wrapper.jar 53KB
MyTwitter.iml 863B
local.properties 413B
共 76 条
- 1
资源评论
- sndtb2019-10-22能运行,但无法实现Twitter登录和分享王小二12042019-10-23你手机安装twitter APP了吗,翻墙了吗?
王小二1204
- 粉丝: 65
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 数据库课程设计-基于的个性化购物平台的建表语句.sql
- 数据库课程设计-基于的图书智能一体化管理系统的建表语句.sql
- Java 代码覆盖率库.zip
- Java 代码和算法的存储库 也为该存储库加注星标 .zip
- 免安装Windows10/Windows11系统截图工具,无需安装第三方截图工具 双击直接使用截图即可 是一款免费可靠的截图小工具哦~
- Libero Soc v11.9的安装以及证书的获取(2021新版).zip
- BouncyCastle.Cryptography.dll
- 5.1 孤立奇点(JD).ppt
- 基于51单片机的智能交通灯控制系统的设计与实现源码+报告(高分项目)
- 什么是 SQL 注入.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功