我正在尝试创建一个聊天应用程序,但是在我使用socket.io的地方,当收到消息时,会触发一个事件,并且应该执行该socket.io事件中的代码,该事件中的代码正在执行但是要更新UI,我使用了不起作用的runOnUiThread,我想在收到新消息时更新messageRecyclerView,但是我无法在常规事件处理程序中执行此操作,因此我必须在UI线程上运行它,而不是工作
public class ChatActivity extends AppCompatActivity {
}
private void init(){
Glide.with(this).load(freind.getProfileUrl()).into(freindProfileImageView);
freindNameTextView.setText(freind.getName());
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("id",freind.getId());
socket.emit("isOnline",jsonObject);
socket.on("online", new Emitter.Listener() {
@Override
public void call(final Object... args) {
if (context != null){
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = (JSONObject) args[0];
Log.d(TAG, "call: " + jsonObject);
JSONObject userObject = jsonObject.getJSONObject("user");
String id = userObject.getString("id");
if (id.equals(freind.getId())){
freindStatusTextView.setText("Online");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
}
});
socket.on("isOnline", new Emitter.Listener() {
@Override
public void call(final Object... args) {
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try{
JSONObject jsonObject = (JSONObject) args[0];
String id = jsonObject.getString("id");
if (id.equals(freind.getId())){
freindStatusTextView.setText("Online");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
isOnline = true;
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
});
socket.on("isOffline", new Emitter.Listener() {
@Override
public void call(final Object... args) {
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try{
JSONObject jsonObject = (JSONObject) args[0];
String id = jsonObject.getString("id");
if (id.equals(freind.getId())){
freindStatusTextView.setText("Offline");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
isOnline = false;
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
});
socket.on("offline", new Emitter.Listener() {
@Override
public void call(final Object... args) {
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = (JSONObject) args[0];
String id = jsonObject.getString("id");
if (id.equals(freind.getId())){
freindStatusTextView.setText("Offline");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
isOnline = false;
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
if (s.length() > 0){
Log.d(TAG, "call: " + freind.getId());
final JSONObject jsonObject = new JSONObject();
jsonObject.put("toId",freind.getId());
socket.emit("typing",jsonObject);
lastTextEdit = System.currentTimeMillis();
handler.postDelayed(inputFinishChecker,delay);
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
socket.on("newConversation", new Emitter.Listener() {
@Override
public void call(final Object... args) {
try{
JSONObject jsonObject = (JSONObject) args[0];
Log.d(TAG, "run: new Conversation " + jsonObject.toString());
String createdAt = jsonObject.getString("createdAt");
String conversationId = jsonObject.getString("_id");
Conversation conversation1 = dbHelper.checkIfConversationExistsById(conversationId);
if (conversation1 == null){
JSONArray usersArray = jsonObject.getJSONArray("Users");
for (int i = 0; i < usersArray.length(); i++){
JSONObject userObject = usersArray.getJSONObject(i);
String id = userObject.getString("user_id");
if (!id.equals(user.getId())){
String username = userObject.getString("username");
String name = userObject.getString("name");
String profileUrl = userObject.getString("profileUrl");
Conversation conversation = new Conversation(conversationId,id,username,name,profileUrl,"","","",createdAt);
dbHelper.addConversation(conversation);
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
`
socket.on("newMessage", new Emitter.Listener() {
@Override
public void call(final Object... args) {
try{
counter++;
Log.d(TAG, "call: new message" + counter);
JSONObject jsonObject = (JSONObject) args[0];
Log.d(TAG, "run: newMessage" + jsonObject.toString());
String id = jsonObject.getString("_id");
Message newMessage = dbHelper.checkIfMessageExists(id);
if (newMessage == null){
String conversationId = jsonObject.getString("conversationID");
String fromId = jsonObject.getString("fromId");
eventMessageFromId = fromId;
String toId = jsonObject.getString("toId");
String content = jsonObject.getString("content");
String createdAt = jsonObject.getString("createdAt");
Date date = new Timestamp(Long.parseLong(createdAt));
String[] splittedArray = date.toString().split(" ");
String[] splittedTime = splittedArray[1].split(":");
String time = splittedTime[0] + ":" + splittedTime[1];
Message message = new Message(id,conversationId,fromId,toId,content,createdAt,time);
dbHelper.addMessage(message);
Date date1 = new Timestamp(Long.parseLong(message.getCreatedAt()));
dbHelper.updateConversation(conversationId,content,createdAt,date1.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: I ran thread");
Log.d(TAG, "run: I ran");
if (eventMessageFromId.equals(freind.getId())){
Log.d(TAG, "run: Message from this freind");
if (messageAdapter != null){
messageAdapter.notifyDataSetChanged();
}
else{
setupMessagesRecyclerView();
}
}
}
});
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
`
socket.on("typing", new Emitter.Listener() {
@Override
public void call(final Object... args) {
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try{
JSONObject jsonObject = (JSONObject) args[0];
String id = jsonObject.getString("id");
if (id.equals(freind.getId())){
freindStatusTextView.setText("typing...");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.green));
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
});
socket.on("stop-typing", new Emitter.Listener() {
@Override
public void call(final Object... args) {
new Thread(){
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try{
JSONObject jsonObject = (JSONObject) args[0];
String id = jsonObject.getString("id");
if (id.equals(freind.getId())){
if (isOnline){
freindStatusTextView.setText("Online");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
}
else{
freindStatusTextView.setText("Online");
freindStatusTextView.setTextColor(ContextCompat.getColor(ChatActivity.this,R.color.gray));
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
}.start();
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
private static String[] split;
public static String decoded(String JWTEncoded) throws Exception {
try {
split = JWTEncoded.split("\\.");
} catch (Exception e) {
//Error
}
return getJson(split[1]);
}
private static String getJson(String strEncoded) throws UnsupportedEncodingException {
byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
return new String(decodedBytes, "UTF-8");
}
private String getDateFromTimeStamp(String timeStamp1){
try{
Timestamp timestamp = new Timestamp(Long.parseLong(timeStamp1));
Date dateObject = timestamp;
String stringDate = dateObject.toString();
stringDate = stringDate.substring(0, stringDate.length() - 3);
return stringDate;
}
catch (Exception e){
e.printStackTrace();
}
return timeStamp1;
}
private void setupMessagesRecyclerView(){
Log.d(TAG, "setupMessagesRecyclerView: Size " + messages.size());
messageAdapter = new MessageAdapter(messages,this,user.getId());
messagesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
messagesRecyclerView.setAdapter(messageAdapter);
}
}