I use Twitter4J to post on Twitter, but you need to open a browser and receive a message from Twitter for my application in order to be able to do this, and it works, but when I return to my application, the activity time is closed. Is there any way to open my activity? Web browsing does not work, since Twitter does not allow you to allow using the usual path.
public void TwitterSend () {
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumer_token, consumer_secret);
EditText edit = (EditText) findViewById(R.id.editText1);
edit.setVisibility(View.VISIBLE);
RequestToken requestToken = null;
try {
requestToken = twitter.getOAuthRequestToken();
System.out.println(requestToken.toString());
} catch (TwitterException e) {
e.printStackTrace();
}
AccessToken accessToken = null;
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse(requestToken.getAuthorizationURL()));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(browserIntent);
System.out.println("Open the following URL and grant access to your account:");
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin = edit.getText().toString();
CountDownTimer timer = new CountDownTimer(900000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
};
timer.start();
while(pin.length()<7)
{
pin = edit.getText().toString();
}
System.out.print(pin);
try{
if(pin.length() > 0){
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
}else{
accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
edit.setVisibility(View.GONE);
}else{
te.printStackTrace();
}
}
Status status = null;
try {
SharedPreferences stats = getSharedPreferences(PREFS_NAME, 0);
String quote = stats.getString("shareQuote", "An error has occured. We are Sorry.");
status = twitter.updateStatus(quote);
} catch (TwitterException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Successfully updated the status to [" + status.getText() + "].", Toast.LENGTH_LONG).show();
System.out.println("Successfully updated the status to [" + status.getText() + "].");
edit.setVisibility(View.GONE);
}
source
share