簡體   English   中英

Android:如何在Facebook登錄中隱​​藏進度圈

[英]Android: How to hide progress circle in Facebook login

我正在使用此方法執行Facebook登錄而不使用fb按鈕Facebook身份驗證而無需登錄按鈕

它運行正常,但在fb登錄期間顯示了帶有黑色背景的進度條,我想從活動com.facebook.LoginActivity

如何避免顯示該活動?我只想在登錄com.facebook.LoginActivity期間從我的應用活動中顯示自己的進度

我和facebook sdk 4.x有同樣的問題。 當我點擊Facebook登錄按鈕時,Facebook活動顯示為半透明,但它顯示進度條。 幸運的是,我們可以在主題中禁用此進度條。 所以Facebook活動被宣布為

<activity
    android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

我們所要做的就是創建一個繼承自Theme.Translucent.NoTitleBar的樣式並隱藏進度條:

<style name="FullyTranslucent" parent="android:Theme.Translucent.NoTitleBar">
    <item name="android:progressBarStyle">@style/InvisibleProgress</item>
</style>

<style name="InvisibleProgress">
    <item name="android:visibility">gone</item>
</style>

現在將活動的主題設置為我們的新主題:

<activity
    android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name"
    android:theme="@style/FullyTranslucent" />

瞧! 登錄前的ProgressBar消失了。

為了進一步了解@ VM4的優秀答案,我修改了他們的方法,使其能夠與SDK版本4.12.0一起正常工作

首先,我將以下內容添加到AndroidManifest.xml

 <activity xmlns:tools="http://schemas.android.com/tools"
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@style/Translucent"
            tools:replace="android:theme"/>

在Android Studio 2.2中,Manifest Merging可能會產生錯誤,抱怨android:theme不能被覆蓋,因為它已經存在。 這可以使用tools:replace="android:theme"解決tools:replace="android:theme" <activity>標簽中的tools:replace="android:theme"

我在/res/values/styles.xml創建了一個自定義樣式

 <style name="Translucent" parent="Translucent.Base"/>

 <style name="Translucent.Base" parent="android:Theme.Translucent.NoTitleBar">
    <item name="android:progressBarStyle">@style/InvisibleProgress</item>
 </style>

這正確地刪除了可怕的Facebook進度對話框。

但是,在5.0(API 21)+設備上,這確實具有在FacebookActivity活動時將最頂部系統欄着色的副作用。

為了解決這個問題,我在res/values-v21/styles.xml添加了一個樣式

<style name="Translucent" parent="Translucent.Base">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style> 

這使主題完全透明並刪除了進度對話框。

最后,建議使用@android:style/Theme.NoDisplay解決方案需要注意的@android:style/Theme.NoDisplay是,這不適用於Android Marshmallow 6.0(API 23)+,並且應該在將來避免使用。

簡單的解決方案只是在registercallback中顯示進度條

看我的代碼

fb_login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {

            progressBar.setVisibility(View.VISIBLE);

            // App code
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            Log.v("Profile ---------   ", response.toString());

                            progressBar.setVisibility(View.GONE);

                            try {

                                if (object!=null){

                                    F_ID = object.getString("id");
                                    if (object.has("first_name"))
                                        Name = object.getString("name");
                                    Log.d(TAG, "onCompleted: Name - "+object.getString("name"));
                                    if (object.has("last_name"))
                                        LastName = object.optString("last_name");
                                    Log.d(TAG, "onCompleted: LastName - "+object.optString("last_name"));
                                    if (object.has("email"))
                                        Email = object.optString("email");
                                    if (object.has("birthday"))
                                        DOB = object.optString("birthday");


                                    ProfilePic = "https://graph.facebook.com/" + F_ID + "/picture?type=large";

                                    Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                    intent.putExtra("Name", object.getString("name"));
                                    intent.putExtra("Email", Email);
                                    intent.putExtra("DOB", DOB);
                                    intent.putExtra("ID", F_ID);
                                    intent.putExtra("ImgURL", ProfilePic);
                                    Log.d(TAG, "onCompleted: Email = "+Email+" Name = "+Name+" FID = "+F_ID);
                                    //sharedpreference is used to store the email, password and the useername
                                    SharedPreferenceManager.setDefaults("email", Email, SigninActivity.this);
                                    SharedPreferenceManager.setDefaults("facebook_id", F_ID, SigninActivity.this);
                                    SharedPreferenceManager.setDefaults("profile_pic", "https://graph.facebook.com/" + F_ID + "/picture?type=large", SigninActivity.this);

                                    if (object.has("name"))
                                    SharedPreferenceManager.setDefaults("username", Name, SigninActivity.this);
                                    Log.d(TAG, "onCompleted: Store shared data");

                                    startActivity(intent);

                                }else
                                    Log.d(TAG, "onCompleted: object is null "+object);


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();

            System.out.println("Facebook Login Successful!");
            System.out.println("Logged in user Details : ");
            System.out.println("--------------------------");
            System.out.println("User ID  : " + loginResult.getAccessToken().getUserId());
            System.out.println("Authentication Token : " + loginResult.getAccessToken().getToken());

        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(), "Login cancelled by user!", Toast.LENGTH_LONG).show();
            System.out.println("Facebook Login Cancel!!");

        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(), "Something went wrong!!", Toast.LENGTH_LONG).show();
            System.out.println("Facebook Login failed!! because of " + e.getCause().toString());
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM