Can I override the autotimeout on Android?
I am using Twitter4J to post to Twitter but you have to open up the browser and get a pin from Twitter for my app to be able to do this and that works but when I return to my app, the activity times out and closes itself. Is there a way to keep my activity open? The webview does not work as Twitter doesn't let you authorize with a regular path.
public void TwitterSend () {
// The factory instance is re-useable and thread safe.
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumer_token, consumer_secret);
//WebView webview = (WebView) findViewById(R.id.webview);
//webview.setVisibility(View.VISIBLE);
//ScrollView sc = (ScrollView) findViewById(R.id.scrollView1);
//sc.setVisibility(View.VISIBLE);
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AccessToken accessToken = null;
//webview.loadUrl("https://api.twitter.com/oauth/authorize");
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()){
Sy开发者_JS百科stem.out.println("Unable to get the access token.");
edit.setVisibility(View.GONE);
}else{
te.printStackTrace();
}
}
//persist to the accessToken for future reference.
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) {
// TODO Auto-generated catch block
e.printStackTrace();
//webview.setVisibility(View.GONE);
//edit.setVisibility(View.GONE);
}
Toast.makeText(getApplicationContext(), "Successfully updated the status to [" + status.getText() + "].", Toast.LENGTH_LONG).show();
System.out.println("Successfully updated the status to [" + status.getText() + "].");
// webview.setVisibility(View.GONE);
edit.setVisibility(View.GONE);
//sc.setVisibility(View.GONE);
/*WebView webview = (WebView) findViewById(R.id.webview);
webview.setVisibility(View.VISIBLE);
Twitter twitter=new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumer_token, consumer_secret);
AccessToken a = new AccessToken(oauth_token, oauth_token_secret);
twitter.setOAuthAccessToken(a);
try {
RequestToken requestToken = twitter.getOAuthRequestToken("https://api.twitter.com/oauth/request_token");
webview.loadUrl("https://api.twitter.com/oauth/authorize");
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
It's generally a bad idea in Android to try to force activities to stay open - the best way to deal with activities being destroyed is to make sure that you are handling all of your activities' lifecycle callbacks properly. This page is helpful: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If you properly save the state of your activity, you shouldn't have problems with the system stopping it.
精彩评论