3

Android アプリケーションに Oauth (twitter、google) を実装していますが、一部のユーザーからログインできないという苦情が寄せられています。問題を分析した後、一部のデバイスでは 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>

FirstActivity.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);
            }
        }
    });

LoginActivity.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