在我的应用程序中,我必须在android中集成twitter和linkedin集成。一切都很好。但问题是,当我点击我的应用程序twitter buttton打开twitter网站时,首先出现黑屏,然后打开twitter网页。
现在,在这里没有做任何事情我按下设备后退按钮它再次显示我黑屏然后再按下后退按钮它来到我的活动。
if(arg0==TWITTER)
{
if(Webservice.isInternetOn(getApplicationContext()))
{
if (!Deja_Twitter_Utils.isAuthenticated(prefs))
{
Intent i=new Intent(this,Prepare_Request_Token.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
}}
在上面的代码中,我写了i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);它没有显示黑屏,但发布消息时有问题。它没有获得授权令牌。通过删除此我能够发布消息。我几天都在努力解决这个问题。
任何帮助都将不胜感激。
以下是启动Twitter连接的代码
public class Prepare_Request_Token extends Activity {
final String TAG = getClass().getName();
private OAuthConsumer consumer;
private OAuthProvider provider;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
this.consumer = new CommonsHttpOAuthConsumer(Constants_Key.CONSUMER_KEY, Constants_Key.CONSUMER_SECRET);
this.provider = new CommonsHttpOAuthProvider(Constants_Key.REQUEST_URL,Constants_Key.ACCESS_URL,Constants_Key.AUTHORIZE_URL);
}
catch (Exception e)
{
Log.e(TAG, "Error creating consumer / provider",e);
}
Log.i(TAG, "Starting task to retrieve request token.");
new OAuth_Request_TokenTask(this,consumer,provider).execute();
}
/**
* Called when the OAuthRequestTokenTask finishes (user has authorized the request token).
* The callback URL will be intercepted here.
*/
@Override
public void onNewIntent(Intent intent)
{
System.out.println("**************************on NEW INTENT****");
super.onNewIntent(intent);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals(Constants_Key.OAUTH_CALLBACK_SCHEME)) {
Log.i(TAG, "Callback received : " + uri);
Log.i(TAG, "Retrieving Access Token");
new RetrieveAccessTokenTask(this,consumer,provider,prefs).execute(uri);
finish();
}
}
public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
private SharedPreferences prefs;
public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) {
this.context = context;
this.consumer = consumer;
this.provider = provider;
this.prefs=prefs;
}
/**
* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret
* for future API calls.
*/
@Override
protected Void doInBackground(Uri...params) {
final Uri uri = params[0];
final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try {
provider.retrieveAccessToken(consumer, oauth_verifier);
final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
System.out.println("*************TOKEN INPREPARE*****"+token);
consumer.setTokenWithSecret(token, secret);
//context.startActivity(new Intent(context,Twitter_Update.class));
//executeAfterAccessTokenRetrieval();
Log.i(TAG, "OAuth - Access Token Retrieved");
} catch (Exception e) {
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
}
return null;
}
private void executeAfterAccessTokenRetrieval()
{
String msg = getIntent().getExtras().getString("tweet_msg");
System.out.println("****************executeAfterAccessTokenRetrieval()***"+msg);
try
{
System.out.println("****************sendTweet()***"+msg);
Deja_Twitter_Utils.sendTweet(prefs, msg);
}
catch (Exception e)
{
Log.e(TAG, "OAuth - Error sending to Twitter", e);
}
}
}
public class OAuth_Request_TokenTask extends AsyncTask<Void, Void, Void> {
final String TAG = getClass().getName();
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
/**
*
* We pass the OAuth consumer and provider.
*
* @param context
* Required to be able to start the intent to launch the browser.
* @param provider
* The OAuthProvider object
* @param consumer
* The OAuthConsumer object
*/
private final ProgressDialog dialog = new ProgressDialog(getDialogContext());
protected void onPreExecute()
{
this.dialog.setMessage("Please Wait..");
this.dialog.show();
}
public OAuth_Request_TokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider)
{
this.context = context;
this.consumer = consumer;
this.provider = provider;
}
/**
*
* Retrieve the OAuth Request Token and present a browser to the user to authorize the token.
*
*/
@Override
protected Void doInBackground(Void... params)
{
try {
Log.i(TAG, "Retrieving request token from Google servers");
final String url = provider.retrieveRequestToken(consumer, Constants_Key.OAUTH_CALLBACK_URL);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
/*************************************************************************/
System.out.println("*************************IN SHOW DIALOGUE************"+url);
/*************************************************************************/
//new Twitter_DIal(context, url,listener).show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
//finish();
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
}
return null;
}
@Override
protected void onPostExecute(Void unused)
{
this.dialog.dismiss();
}
}
private Context getDialogContext()
{
Context context;
if (getParent() != null) context = getParent();
else context = this;
return context;
}
@Override
public void onBackPressed()
{
System.out.println("****************onBackPressed() prepare request token***");
finish();
return;
}