3

我正在为 Android 应用程序实施 Oauth(推特、谷歌),一些用户抱怨他们无法登录;在分析问题后,我发现在某些设备中,有时不会调用 onNewIntent() 而是调用 onCreate() 方法。因此,似乎活动的任务、实例、..

这是我的代码:

AndroidManifest.xml

<activity
            android:name="LoginActivity"
            android:launchMode="singleTask" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="callback"
                    android:scheme="x-oauthflow" />
            </intent-filter>
        </activity>
        <activity
            android:name="FirstActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

第一活动.java

    btnGoogle = (Button)findViewById(R.id.btnGoogle);
    btnGoogle.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Utils.bInternetConnection(getApplicationContext())) {
                Intent intentGoogle = new Intent(getApplicationContext(), LoginActivity.class);

                Bundle b = new Bundle();
                b.putInt(Constants.SOCIAL_NETWORK, Constants.SOCIAL_NETWORK_GOOGLE);
                intentGoogle.putExtras(b);
                startActivity(intentGoogle);
            } else {
                Utils.showDialog(FirstActivity.this, R.string.no_internet_title, R.string.no_internet_msg);
            }
        }
    });
    btnTwitter = (Button)findViewById(R.id.btnTwitter);
    btnTwitter.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Utils.bInternetConnection(getApplicationContext())) {
                Intent intentTwitter = new Intent(getApplicationContext(), LoginActivity.class);

                Bundle b = new Bundle();
                b.putInt(Constants.SOCIAL_NETWORK, Constants.SOCIAL_NETWORK_TWITTER);
                intentTwitter.putExtras(b);
                startActivity(intentTwitter);
            } else {
                Utils.showDialog(FirstActivity.this, R.string.no_internet_title, R.string.no_internet_msg);
            }
        }
    });

登录活动.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    // Async Load
    new loadPage(this).execute("");

}

/**
 * The callback URL will be intercepted here.
 */
@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    ....
    ....

}


private class loadPage extends AsyncTask<String, Integer, Void> {

    private Context context;

     public loadPage(Context context) {
            this.context = context;
     }

    private ProgressDialog pdia;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdia = new ProgressDialog(LoginActivity.this);
        pdia.setMessage(getString(R.string.loading));
        pdia.setCanceledOnTouchOutside(false);
        pdia.show();
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            Bundle b = getIntent().getExtras();
            int socialNetwork = b.getInt(Constants.SOCIAL_NETWORK);
            _socialNetwork = socialNetwork;

            switch (_socialNetwork) {
            case Constants.SOCIAL_NETWORK_GOOGLE:
                consumer_key = Constants.CONSUMER_KEY_GOOGLE;
                consumer_secret = Constants.CONSUMER_SECRET_GOOGLE;
                url_request_token = Constants.URL_REQUEST_TOKEN_GOOGLE
                        + "?scope="
                        + URLEncoder
                                .encode(Constants.SCOPE_GOOGLE, "utf-8");
                url_access_token = Constants.URL_ACCESS_TOKEN_GOOGLE;
                url_authorize_token = Constants.URL_AUTHORIZE_TOKEN_GOOGLE;
                break;
            case Constants.SOCIAL_NETWORK_TWITTER:
                consumer_key = Constants.CONSUMER_KEY_TWITTER;
                consumer_secret = Constants.CONSUMER_SECRET_TWITTER;
                url_request_token = Constants.URL_REQUEST_TOKEN_TWITTER;
                url_access_token = Constants.URL_ACCESS_TOKEN_TWITTER;
                url_authorize_token = Constants.URL_AUTHORIZE_TOKEN_TWITTER;
                break;
            }

            // Google & Twitter

                consumer = new CommonsHttpOAuthConsumer(consumer_key,
                        consumer_secret);

                provider = new CommonsHttpOAuthProvider(url_request_token,
                        url_access_token, url_authorize_token);

                // Get authUrl
                String authUrl = provider.retrieveRequestToken(consumer,
                        callBack.toString());

                // Bring the user to authUrl
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(authUrl))
                        .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                                | Intent.FLAG_ACTIVITY_NO_HISTORY
                                | Intent.FLAG_FROM_BACKGROUND);
                context.startActivity(intent);


        } catch (Exception e) {
            Log.e(Constants.TAG, "Oauth error", e);
            // Redirect to Login Page
            Intent intentFirst = new Intent(getApplicationContext(),
                    FirstActivity.class);
            startActivity(intentFirst);
            finish();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
        if (pdia.isShowing()){
            pdia.dismiss();
        }           
    }
}

有任何想法吗?

4

0 回答 0