-1

我在 twitter android 链接应用程序中使用了两个活动。第一个活动是 LoginActivity,第二个是 TweetslistActivity。我在 loginActivity 中使用了一个按钮,点击它,Twitter 登录对话框打开。当用户成功登录时,我的应用程序应该打开第二个活动。但是在用户通过身份验证后,我的第一个活动恢复,我必须再次单击登录按钮才能进入我的第二个活动!

 the logcat shows:
08-30 05:05:48.477: I/dalvikvm-heap(4205): Grow heap (frag case) to 6.686MB for 4194320-byte allocation
08-30 05:05:48.557: D/dalvikvm(4205): GC_FOR_ALLOC freed 0K, 3% free 6732K/6920K, paused 76ms, total 76ms
08-30 05:05:49.288: D/gralloc_goldfish(4205): Emulator without GPU emulation detected.
08-30 05:05:55.757: V/tweetsData--(4205): null
08-30 05:06:11.897: E/Twitter Login Error(4205): > No Token available.

我还在清单文件中使用了 'android:launchMode="singleTask"'。但它只是将我从第一个活动引导到第二个活动而无需登录!我只是卡住但没有得到任何答案..请提出一些好的解决方案。先感谢您。

编辑:

原始代码:

public class TwitterLoginActivity extends Activity {
    TextView tv;
    Button loginBtn;
    ImageView logo;
    private ConnectionDetector cd;
    static Twitter twitter;
    ProgressDialog pDialog;
    ArrayList<String> tweets;
    String tweetsData;

    private static SharedPreferences mSharedPreferences;
    AlertDialogManager alert = new AlertDialogManager();
    RequestToken requestToken;
    AccessToken accessToken;
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.twitter_login);
            tweets = new ArrayList<String>();
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            if (android.os.Build.VERSION.SDK_INT > 9) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll().build();
                    StrictMode.setThreadPolicy(policy);
            }
            cd = new ConnectionDetector(getApplicationContext());
            if (!cd.isConnectingToInternet()) {
                    alert.showAlertDialog(TwitterLoginActivity.this,
                                    "Internet Connection Error",
                                    "Please connect to working Internet connection", false);
                    return;
            }

            if (CONSUMER_KEY.trim().length() == 0
                            || CONSUMER_SECRET.trim().length() == 0) {
                    // Internet Connection is not present
                    alert.showAlertDialog(TwitterLoginActivity.this, "Twitter oAuth tokens",
                                    "Please set your twitter oauth tokens first!", false);
                    return;
            }

            tv = (TextView) findViewById(R.id.welcome_txt);
            loginBtn = (Button) findViewById(R.id.login_btn);
            logo = (ImageView) findViewById(R.id.imageView1);
            mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref",
                            0);

            loginBtn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            Thread th = new Thread(new Runnable() {
                                    public void run() {
                                            loginToTwitter();
                                    }
                            });
                            th.start();

                    }
            });

            if (!isTwitterLoggedInAlready()) {
                    Uri uri = getIntent().getData();
                    if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
                            uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

                            try {

                                    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken);
                                    Editor e = mSharedPreferences.edit();
                                    e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                                    e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret());
                                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                                    e.commit();

                                    Log.e("Twitter OAuth Token", "> " + accessToken.getToken());

                            }
                            catch (Exception e) {
                                    Log.e("Twitter Login Error", "> " + e.getMessage());
                            }
                    }
            }

    }

    public void loginToTwitter() {
            if (!isTwitterLoggedInAlready()) {
                    ConfigurationBuilder builder = new ConfigurationBuilder();
                    builder.setOAuthConsumerKey(CONSUMER_KEY);
                    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
                    Configuration configuration = builder.build();
                    TwitterFactory factory = new TwitterFactory(configuration);
                    twitter = factory.getInstance();
                    try {
                            requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
                            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                            .parse(requestToken.getAuthenticationURL()))); }
                            });

                    }
                    catch (TwitterException e) {
                            e.printStackTrace();
                    }
            }
                    Intent intent = new Intent(TwitterLoginActivity.this, TweetsListActivity.class);
                    intent.putExtra("tweetsdata",tweetsData);
                    startActivity(intent);

                    finish();               }


    private boolean isTwitterLoggedInAlready() {
            return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
    }

    @Override
    protected void onResume() {
            super.onResume();

    }

}
4

1 回答 1

0

将 onCreate() 末尾的代码与我的更改一起放入 onResume() :

Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
    uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

    try {
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken);
        Editor e = mSharedPreferences.edit();
        e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
        e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret());
        e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
        e.commit();

        Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
    }
    catch (Exception e) {
        Log.e("Twitter Login Error", "> " + e.getMessage());
    }
}

if (!isTwitterLoggedInAlready()) {
    // Goto TweetsListActivity
    Intent intent = new Intent(TwitterLoginActivity.this, TweetsListActivity.class);
    intent.putExtra("tweetsdata",tweetsData);
    startActivity(intent);
    finish();
}

另请注意,在第 127-128 行的代码中,这些行已切换:

     startActivity(intent);
     intent.putExtra("tweetsdata",tweetsData);

开始活动后,改变意图是没有意义的。

编辑:

在用于网络登录的 startActivity() 之后还注意到此代码,这没有任何意义:

TwitterLoginActivity.this.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(TwitterLoginActivity.this,
                TweetsListActivity.class);
        intent.putExtra("tweetsdata",tweetsData);
        startActivity(intent);
        finish();
    }
});

删除此代码。

于 2013-08-30T10:27:07.987 回答