我正在处理的应用提示用户在打开应用时使用Firebase登录。如果应用程序完全关闭,则在打开时,用户将进入登录屏幕。但是,我工作的公司的许多工作人员都没有关闭应用程序,他们只是将它发送到后台。这是一个问题,因为如果我删除他们的帐户,它不会立即将其删除;它只会在关闭应用程序后将它们踢出去。
为了解决这个问题,我基本上对主屏幕进行了编码,以便每次页面重新加载 - 无论是到达前台,从不同的活动返回主页,还是再次启动应用程序 - 它都会在后台登录用户,然后重新登录,然后检查用户帐户的有效性。
这样做的最终目标是,如果用户仍然拥有有效的登录信息,他们将不会受到影响,但如果他们的登录信息无效,即使他们没有完全关闭应用,他们也会被锁定。
但是,即使已经输入了有效的登录信息,目前应用程序也会将用户踢回登录界面。
主要活动代码(登录屏幕):
package com.priceelectric.xriley.priceprefab;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);
final SharedPreferences.Editor mEditor = prefs.edit();
mAuth = FirebaseAuth.getInstance();
FirebaseAuth.getInstance().signOut();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null){
Log.d("loginTag", "onAuthStateChanged:signed_in:" + user.getUid());
Context context = getApplicationContext();
final Intent returnHome = new Intent();
returnHome.setClass(context, Home_Screen.class);
startActivity(returnHome);
}
else{
Log.d("loginTag", "onAuthStateChanged:signed_out");
}
}
};
final EditText usernameTextBox = (EditText) findViewById(R.id.usernameTextBox);
final EditText passwordTextBox = (EditText) findViewById(R.id.passwordTextBox);
usernameTextBox.setText(prefs.getString("username", ""));
passwordTextBox.setText(prefs.getString("password", ""));
final Button login = (Button) findViewById(R.id.loginButton);
assert login != null;
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditor.putString("username", usernameTextBox.getText().toString());
mEditor.putString("password", passwordTextBox.getText().toString());
mEditor.commit();
prefs.getInt("thing", 0);
final String email = usernameTextBox.getText().toString();
final String password = passwordTextBox.getText().toString();
// FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// final String uid = user.getUid();
// final SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);
// final SharedPreferences.Editor mEditor = prefs.edit();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("loginTag", "signInWithEmail:onComplete:" + task.isSuccessful());
if(!task.isSuccessful()){
Log.w("loginTag", "signInWithEmail:failed", task.getException());
}
}
});
}
});
}
@Override
public void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop(){
super.onStop();
if(mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
@Override
public void onBackPressed(){
Context context = getApplicationContext();
CharSequence notificationText = "You need to log in.";
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, notificationText, duration).show();
}
}
主屏幕代码:
package com.priceelectric.xriley.priceprefab;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Home_Screen extends AppCompatActivity {
private FirebaseAuth mAuth;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home__screen);
final Button paperworkButton = (Button) findViewById(R.id.paperworkButton);
mAuth = FirebaseAuth.getInstance();
final TextView versionLabel = (TextView) findViewById(R.id.versionLabel);
SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);
FirebaseAuth.getInstance().signOut();
mAuth.signInWithEmailAndPassword(prefs.getString("username", ""), prefs.getString("password", ""));
user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null){
//all good
}
else{
Context context = getApplicationContext();
CharSequence notificationText = "You need to log in.";
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, notificationText, duration).show();
final Intent loginScreen = new Intent();
loginScreen.setClass(this, MainActivity.class);
startActivity(loginScreen);
}
if(prefs.getString("yourName", "").equals("")){
final Intent settingsScreen = new Intent();
settingsScreen.setClass(this, Settings_Screen.class);
startActivity(settingsScreen);
}
}
public void buttonOnClick(View view){
SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);
switch(view.getId()){
case R.id.prefabOrderButton:
SharedPreferences.Editor mEditor = prefs.edit();
mEditor.putInt("itemNumberCounter", 0);
mEditor.commit();
final Intent prefabScreen = new Intent();
prefabScreen.setClass(this, Prefab_Order.class);
startActivity(prefabScreen);
break;
case R.id.safetyFormButton:
final Intent safetyReportScreen = new Intent();
safetyReportScreen.setClass(this, Safety_Report.class);
startActivity(safetyReportScreen);
break;
case R.id.resiPanelButton:
final Intent resiPanelScreen = new Intent();
resiPanelScreen.setClass(this, Resi_Panel_Builder.class);
startActivity(resiPanelScreen);
break;
case R.id.onlineResourcesButton:
final Intent onlineResourcesScreen = new Intent();
onlineResourcesScreen.setClass(this, Online_Resources.class);
startActivity(onlineResourcesScreen);
break;
case R.id.settingsButton:
final Intent settingsScreen = new Intent();
settingsScreen.setClass(this, Settings_Screen.class);
startActivity(settingsScreen);
break;
case R.id.paperworkButton:
final Intent paperworkScreen = new Intent();
paperworkScreen.setClass(this, Paperwork_Orders.class);
startActivity(paperworkScreen);
break;
}
}
@Override
public void onBackPressed(){
//do nothing
}
}
建议将不胜感激。
答案 0 :(得分:0)
在namespace ConsoleApplicationGrbage
{
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
var list = new Dictionary<string, string>();
list.Add("NameA", "YourClass");
var thisAssemebly = Assembly.GetExecutingAssembly();
var exT = thisAssemebly.ExportedTypes;
//register types only that are in the dictionary
foreach (var item in list)
{
var typeClass = exT.First(x => x.Name == item.Value);
var ivmt = Type.GetType("ConsoleApplicationGrbage.IYourInterface");
// --> Map Interface to ImplementationType
container.RegisterType(ivmt, typeClass, item.Key);
// or directly:
container.RegisterType(typeof(IYourInterface), typeClass, item.Key);
}
var impl = container.Resolve<IYourInterface>("NameA");
}
}
public interface IYourInterface
{
}
public class YourClass: IYourInterface
{
}
}
活动中,如果您向<script type="text/javascript">
getReadXmlFile();
function getReadXmlFile(){
alert("recherche d fichier");
$.ajax({
type: "GET",
url: "http://www.velib.paris/service/stationdetails/paris/901",
dataType: "xml",
success: parseXml
});
alert("obtention du fichier");
}
function parseXml(xml){
alert('debut du parse');
var up=$(xml).find("updated").text();
alert(up);
}
</script>
添加完成侦听器,我认为您会获得更好的结果,类似于Home_Screen
中的内容。
使用现有代码:
signInWithEmailAndPassword()
您在登录完成之前请求当前用户,因此MainActivity
为空。
添加完成侦听器并移动代码以生成Toast并在那里启动 mAuth.signInWithEmailAndPassword(prefs.getString("username", ""), prefs.getString("password", ""));
user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null){
//all good
}
,在user
为false时运行它。
此外,您可能希望尝试使用FirebaseUser.reload(),而不是使用注销/登录来测试用户帐户是否仍然有效。文档表明如果当前用户帐户被禁用或删除,它将返回失败结果。
使用完成侦听器获取MainActivity
状态:
task.isSuccessful()