我已经在android应用程序上创建。我需要使用我自己的twitter登录凭据,从我的android应用程序直接向我的twitter帐户发布推文。我已经在twitter上注册了,并获得了消费者密钥和消费者密钥。有了这个,我希望我的应用程序应该授权我的twitter帐户,我应该能够以编程的方式发布推文,而没有任何弹出窗口。致敬库纳尔
发布于 2020-01-02 17:44:10
解决方案是创建一个用于推文的Custom Webview
。它甚至不需要Fabric Twitter API。
1-创建Webview活动:
public class TweetCustomWebView extends AppCompatActivity {
android.webkit.WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
final String stringToShow = extras.getString("tweettext");
webView = (android.webkit.WebView) findViewById(R.id.wv);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
if (url.contains("latest_status_id=")) {
// Twitted
setResult(Activity.RESULT_OK, new Intent());
TweetCustomWebView.this.finish();
}
view.loadUrl(url);
return true;
}
public void onPageFinished(android.webkit.WebView view, String url) {
// Finished loading url
}
public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
Log.e("", "Error: " + description);
setResult(Activity.RESULT_CANCELED, new Intent());
}
});
webView.loadUrl("https://twitter.com/intent/tweet?text=" + stringToShow);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
setResult(Activity.RESULT_CANCELED, new Intent());
}}
2-像这样的布局:
<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/wv"/>
3-在AndroidManifest.xml
中,我们必须将webview
的活动添加到<application>
标记中:
<activity android:name=".TweetCustomWebView" />
4-最后一步是当用户点击在Twitter上分享按钮时调用我们的webview:
Intent intent = new Intent(MainActivity.this, TweetCustomWebView.class);
intent.putExtra("tweettext", "Text to tweet");
startActivityForResult(intent, 100);
或者您可以使用Account Activity API用于自定义推文用途。
应该就是这样。我希望这能有所帮助。
https://stackoverflow.com/questions/59560933
复制相似问题