现在我正在尝试使用Firebase制作像instagram聊天程序一样的程序 问题是我想显示该会员的登录状态 但是android的生命周期是个问题
这是我的代码HomeActiviy 用户登录成功后,请转到HomeActivty 从这里我确定是在线还是离线
这是我的在线或离线代码,
private void status(String status){
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("status", status);
reference.updateChildren(hashMap);
}
@Override
protected void onResume() {
super.onResume();
status("online");
}
@Override
protected void onPause() {
super.onPause();
status("offline");
}
从我的HomeActivity.java中,当推聊天ImageButton时 转到聊天活动 他们会看到彼此的在线图像按钮为绿色或灰色
这是我的问题 当从HomeActivity转到另一个活动时 状态将显示为脱机
第二次点击聊天按钮
这是我的聊天活动
package com.example.together.activities.chat;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.together.fragment.ChatFragment;
import com.example.together.fragment.UsersFragment;
import com.example.together.model.User;
import com.example.together.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import de.hdodenhof.circleimageview.CircleImageView;
public class ChatsActivity extends AppCompatActivity {
CircleImageView image_profile;
TextView username;
FirebaseUser firebaseUser;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
image_profile = findViewById(R.id.image_profile);
username = findViewById(R.id.username);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if (user.getImageurl().equals("default")){
image_profile.setImageResource(R.mipmap.ic_launcher);
}else {
Glide.with(ChatsActivity.this).load(user.getImageurl()).into(image_profile);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new ChatFragment(), "채팅");
viewPagerAdapter.addFragment(new UsersFragment(), "친구찾기");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
private ArrayList<String> titles;
ViewPagerAdapter(FragmentManager fm){
super(fm);
this.fragments = new ArrayList<>();
this.titles = new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment, String title){
fragments.add(fragment);
titles.add(title);
}
// Ctrl + O
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
}
这是我的错误
2019-07-09 16:46:29.993 13773-13773/com.example.blogapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blogapp, PID: 13773
java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:323)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:132)
at com.bumptech.glide.Glide.with(Glide.java:741)
at com.example.together.activities.chat.ChatsActivity$1.onDataChange(ChatsActivity.java:66)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我认为这与活动的生命周期有关
我如何保持身份?
答案 0 :(得分:2)
将此依赖项放入您的build.gradle
文件中:
implementation "android.arch.lifecycle:extensions:*"
然后在您的Application类中,使用此:
public class MyApplication extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
Log.d("MyApp", "App in background");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
Log.d("MyApp", "App in foreground");
}
}
更新您的AndroidManifest.xml文件:
<application
android:name=".MyApplication"
....>
</application>
当您的应用程序在后台更改为
offline
时,其状态为online
。
答案 1 :(得分:1)
问题似乎是您的ChatsActivity
被破坏了,而Glide试图加载图像。您可以使用getApplicationContext()
获取当前上下文。
尝试更换,
Glide.with(ChatsActivity.this).load(user.getImageurl()).into(image_profile);
使用
Glide.with(getApplicationContext()).load(user.getImageurl()).into(image_profile);
此外,您应该使用firebase提供的.onDisconnect()
方法来检查用户的存在
建立onDisconnect()操作时,该操作位于Firebase实时数据库服务器上。服务器会检查安全性,以确保用户可以执行请求的写入事件,并通知您的应用程序是否无效。然后,服务器监视连接。如果在任何时候连接超时或被Realtime Database客户端主动关闭,服务器将再次检查安全性(以确保操作仍然有效),然后调用事件。
您可以从此doc
中找到更多信息答案 2 :(得分:0)
只要用户在您的应用中,从技术上讲,他就在线。因此,在您的家庭活动中使用以下方法:
@Override
protected void onStart() {
super.onStart();
myRef.child(uid).child("isOnline").setValue(true);
myRef.child(uid).child("isOnline").onDisconnect().setValue(false);
}
我也建议添加以覆盖onResume():
@Override
protected void onResume() {
super.onResume();
myRef.child(uid).child("isOnline").setValue(true); //User back online
}
确保添加if语句以检查用户是否连接到互联网!