这里有一项名为groupchat
的活动。我在这里使用静态对象activeEventMO
。
在这个群聊活动中,聊天功能正在进行中。如果user1向user2发送消息,则
gcmIntent
类收到该消息。在这个gcmIntent类中,我添加了通知代码。这里,如果user1发送针对user2收到的特定事件通知的消息。如果user2单击该通知,则用户将转到该聊天窗口,这意味着群聊活动。
在gcmIntent类中,我使用parcelable将eventMO
发送到groupchat活动。如果user1向event2发送消息,则event2 user2收到通知。如果user2 clliks通知它显示event1的chatwindow。因为我第一次通过唱歌eventMO发送eventid1。所以它永远保留了eventid1。它无法刷新。
这是我的群聊活动代码
public class GroupChatActivity extends Activity {
private static ListView listViewMessages;
private static List<ChatMO> listChatMessageObjectses = new ArrayList<ChatMO>();
private static MessagesListAdapter adapter;
private static Handler UIHandler;
private static int RESULT_LOAD_IMAGE = 1;
private EditText inputMsg;
private String formatDate;
private Context context;
private String dateResult;
private static EventMO activeEventMO = null;
private UserDelegate userDelegate = new UserDelegate();
private MediaDelegates mediaDelegates = new MediaDelegates();
private MessageDelegates messageDelegates = new MessageDelegates();
private SharedPreferences sharedpreferences;
private List<UserMO> eventUserMOs = new ArrayList<>();
private Gson gson = new Gson();
private UserMO userMO = new UserMO();
private MessageMO messageMO = new MessageMO();
private File imgFile;
private MediaMO mediaMO = new MediaMO();
// this method is for store the current event id to eventMO coz eventMO globally declared as static
public void getevent()
{
activeEventMO = (EventMO) getIntent().getParcelableExtra("eventMo");
}
public static void messageHandler(final MessageMO messageMo) {
UIHandler = new Handler(Looper.getMainLooper());
UIHandler.post(new Runnable() {
public void run() {
Log.e("messageHandler", messageMo.getEventTitle());
ChatMO chatMO = new ChatMO();
chatMO.setMessage(messageMo.getMessage());
chatMO.setSelf(0);
chatMO.setFromName(messageMo.getfromUserName());
chatMO.setDate(messageMo.getDate());
Log.e("handler", "eventMO" + activeEventMO);
Log.e("handler", "messageMO" + messageMo);
if (activeEventMO.getEventId() == messageMo.getEventId()) {
listChatMessageObjectses.add(chatMO);
listViewMessages.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("APPSTATUS", 1);
editor.putLong("eventId", activeEventMO.getEventId());
editor.commit();
Log.i("App", "start");
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
@Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
@Override
protected void onResume() {
super.onPause();
AppActivityStatus.setActivityStarted();
}
@Override
protected void onStop() {
super.onStop();
Log.i("App", "stop");
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("APPSTATUS", 2);
editor.commit();
AppActivityStatus.setActivityStoped();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.image_upload, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ChatMO chatMO = new ChatMO();
chatMO.setMessage_type(Constants.IS_TYPE_CHAT_IMAGE);
chatMO.setMessage(picturePath);
chatMO.setSelf(1);
chatMO.setIs_delete(0);
chatMO.setFromName("");
chatMO.setEvent_id(activeEventMO.getEventId());
messageMO.setMessage("");
messageMO.setRingeeUserId(userMO.getRingeeUserId());
// messageMO.setDate(messageMO.getDate());
messageMO.setMobileNumber(userMO.getMobileNumber());
messageMO.setEventId(activeEventMO.getEventId());
messageMO.setEventTitle(activeEventMO.getText());
Log.e("sendbutton", "eventtitle" + messageMO.getEventTitle());
messageMO.setfromUserName(userMO.getUserName());
messageMO.setMessageType(Constants.MESSAGE_TYPE_MSG);
mediaMO.setRingeeUserId(userMO.getRingeeUserId());
mediaMO.setIsType(Constants.IS_TYPE_CHAT_IMAGE);
imgFile = new File(picturePath);
listChatMessageObjectses.add(chatMO);
listViewMessages.setAdapter(adapter);
adapter.notifyDataSetChanged();
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return mediaDelegates.insertChatFile(context, imgFile, messageMO, eventUserMOs, mediaMO);
}
@Override
protected void onPostExecute(String mediaBo) {
Toast.makeText(getApplicationContext(), "uploaded Status" + mediaBo, Toast.LENGTH_LONG).show();
if (!mediaBo.equals("null")) {
MediaMO mediaMO1 = gson.fromJson(mediaBo, new TypeToken<MediaMO>() {
}.getType());
}
}
}.execute(null, null, null);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.ic_menu_gallery) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final List<ChatMO> chatMOs1 = new ArrayList<>();
activeEventMO = new EventMO();
setContentView(R.layout.chat_main);
context = getApplicationContext();
String message = "";
DatabaseHelper dbHelper = new DatabaseHelper(context);
listChatMessageObjectses = chatMOs1;
inputMsg = (EditText) findViewById(R.id.inputMsg);
listViewMessages = (ListView) findViewById(R.id.list_view_messages);
activeEventMO = (EventMO) getIntent().getParcelableExtra("eventMo");
Log.e("oncreate", "eventMO" +activeEventMO);
List<ChatMO> chatMOs = dbHelper.getGroupChatMessageForEvent(activeEventMO.getEventId());
Log.e("oncreate", "chatMOs" + chatMOs);
for (ChatMO chatMO1 : chatMOs) {
message = chatMO1.getMessage();
chatMO1.getEvent_id();
chatMO1.getFromName();
int messageType = chatMO1.getMessage_type();
chatMO1.getDate();
chatMO1.isSelf();
if (messageType == 0) {
chatMOs1.add(chatMO1);
}
}
adapter = new MessagesListAdapter(context, chatMOs1);
//adapter functionality added for show the previous chat list of event/invite
listViewMessages.setAdapter(adapter);
sharedpreferences = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, context.MODE_PRIVATE);
// by default first primary user is current user in sql lite
// user table
userMO = dbHelper.getUserData(1);
这里我在gcmIntent
中传递eventMOIntent groupChatActFrag = new Intent(getApplicationContext(), GroupChatActivity.class);
EventMO eventMO = new EventMO();
eventMO.setEventId(messageMO.getEventId());
Log.e("gcm","evetid"+eventMO.getEventId());
groupChatActFrag.putExtra("eventMo", eventMO);
groupChatActFrag.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("MessageMO", gson.toJson(messageMO));
editor.commit();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, groupChatActFrag, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_dialog_info).setContentTitle(messageMO.getEventTitle())
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageMO.getfromUserName())).setContentText(messageMO.getMessage()).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
Log.e("gcm","eventid"+messageMO.getEventId());
mNotificationManager.notify((int) (long) eventMO.getEventId(), mBuilder.build());
}
您能否提前告诉我如何解决此问题
答案 0 :(得分:1)
要在活动或片段被销毁后刷新变量,您需要存储在方法中提供的已保存捆绑中
public void onSavedInstanceState(final Bundle bundle)
并重置值使用方法中提供的包
public void onCreate(final Bundle savedInstance)
这是一些很好的文档
enter code here[Recreating an Activity - Android][1]