[英]Android: How to hide progress circle in Facebook login
I am using this method to perform a Facebook login without using the fb button Facebook authentication without login button 我正在使用此方法执行Facebook登录而不使用fb按钮Facebook身份验证而无需登录按钮
It's working fine, but a progress bar with black background is shown during fb login, I guess from activity com.facebook.LoginActivity 它运行正常,但在fb登录期间显示了带有黑色背景的进度条,我想从活动com.facebook.LoginActivity
How can I avoid displaying that activity?, I just want to show my own progress from my app activity during login in com.facebook.LoginActivity 如何避免显示该活动?我只想在登录com.facebook.LoginActivity期间从我的应用活动中显示自己的进度
I had the same problem with facebook sdk 4.x. 我和facebook sdk 4.x有同样的问题。 When I click the facebook login button the Facebook Activity appears translucent but it shows a progress bar.
当我点击Facebook登录按钮时,Facebook活动显示为半透明,但它显示进度条。 Luckily we can disable this progress bar in the theme.
幸运的是,我们可以在主题中禁用此进度条。 So the Facebook Activity is declared as
所以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" />
All we have to do is create a style that inherits from Theme.Translucent.NoTitleBar
and hides the progress bar: 我们所要做的就是创建一个继承自
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>
Now set the theme of the activity to our new theme: 现在将活动的主题设置为我们的新主题:
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@style/FullyTranslucent" />
Voila! 瞧! The ProgressBar before login is gone.
登录前的ProgressBar消失了。
To further @VM4's excellent answer I modified their approach for it to work correctly with SDK version 4.12.0 为了进一步了解@ VM4的优秀答案,我修改了他们的方法,使其能够与SDK版本4.12.0一起正常工作
Firstly I added the following to AndroidManifest.xml
首先,我将以下内容添加到
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"/>
In Android Studio 2.2, it is likely that Manifest Merging may produce an error complaining that the android:theme
cannot be overridden as it already exists. 在Android Studio 2.2中,Manifest Merging可能会产生错误,抱怨
android:theme
不能被覆盖,因为它已经存在。 This can be resolved using tools:replace="android:theme"
in the <activity>
tag. 这可以使用
tools:replace="android:theme"
解决tools:replace="android:theme"
<activity>
标签中的tools:replace="android:theme"
。
I created a custom style within /res/values/styles.xml
我在
/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>
This correctly removed the hideous Facebook progress dialog. 这正确地删除了可怕的Facebook进度对话框。
However, on 5.0 (API 21)+ devices this did have the side effect of coloring the top most system bar black for the time the FacebookActivity
was active. 但是,在5.0(API 21)+设备上,这确实具有在
FacebookActivity
活动时将最顶部系统栏着色的副作用。
To fix this I added a style in res/values-v21/styles.xml
为了解决这个问题,我在
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>
This made the theme completely transparent and removed the progress dialog. 这使主题完全透明并删除了进度对话框。
Finally, one thing to note with solutions that recommend using @android:style/Theme.NoDisplay
is that this will not work on Android Marshmallow 6.0 (API 23)+ and should probably be avoided in future. 最后,建议使用
@android:style/Theme.NoDisplay
解决方案需要注意的@android:style/Theme.NoDisplay
是,这不适用于Android Marshmallow 6.0(API 23)+,并且应该在将来避免使用。
Simple solution just show progressbar in registercallback 简单的解决方案只是在registercallback中显示进度条
See my code 看我的代码
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.