如何在android

时间:2015-12-10 07:23:55

标签: java android

此处群聊活动正在运行。 群聊活动表示聊天窗口。在该聊天窗口中,我从sqlite检索了该消息,并由list view adpater显示。在这里,我正在为一个事件进行群聊,当新消息到达其他事件时发出通知。当我点击通知时,即使我从其他类或活动中传递了意图,我的群聊活动也不会再次执行。

这是我的groupchatActivity代码

 @Override
    protected void onStart() {
        super.onStart();
        if (activeEventMO != null) {
            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();

    }


    protected  void onNewIntent(Intent intent){

        super.onNewIntent(intent);
        processExtraData();

    }

    private void processExtraData() {
        activeEventMO = (EventMO) getIntent().getParcelableExtra("eventMo");
    }

    @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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final List<ChatMO> chatMOs1 = new ArrayList<>();
        setContentView(R.layout.chat_main);
        EventMO eventMO = new EventMO();
        context = getApplicationContext();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        listChatMessageObjectses = chatMOs1;
        inputMsg = (EditText) findViewById(R.id.inputMsg);
        listViewMessages = (ListView) findViewById(R.id.list_view_messages);
        //this activeEventMO is a static object
        processExtraData();
       // activeEventMO = (EventMO) getIntent().getParcelableExtra("eventMo");
         List<ChatMO> chatMOs = dbHelper.getGroupChatMessageForEvent(activeEventMO.getEventId());
        //this for loop is for avoiding the notification coz notification also stores groupchat table in sqlite
        for (ChatMO chatMO1 : chatMOs) {
            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);


       /* if (!sharedpreferences.getString("MessageMO", "null").equals("null")) {
            MessageMO messageMO1 = (MessageMO) gson.fromJson(sharedpreferences.getString("MessageMO", "null"), new TypeToken<MessageMO>() {
            }.getType());
            eventMO.setEventId(messageMO1.getEventId());
            Log.e("shared","eventID"+messageMO1.getEventId());
            Log.e("shared","eventID"+eventMO.getEventId());
            eventMO.setText(messageMO1.getEventTitle());
            Log.i("message values", messageMO1.toString());
            ChatMO chatMO = new ChatMO();
            //chatMessageObjects.setMessage(messageMO1.getMessage());
            chatMO.setSelf(0);
            chatMO.setFromName(messageMO1.getfromUserName());
            listChatMessageObjectses.add(chatMO);
            //listViewMessages.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }*/

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... arg0) {
                return userDelegate.getUsersForEvent(activeEventMO.getEventId());
            }

            @Override
            protected void onPostExecute(String eventLists) {
                eventUserMOs = gson.fromJson(eventLists, new TypeToken<ArrayList<UserMO>>() {
                }.getType());
                Toast.makeText(getApplicationContext(), "event users " + eventUserMOs.size(), Toast.LENGTH_LONG).show();
            }
        }.execute(null, null, null);
        Button sendButton = (Button) findViewById(R.id.btnSend);

gcmIntent我在这里传递了gcmIntent

的意图
Intent groupChatActFrag = new Intent(getApplicationContext(), GroupChatActivity.class);
            EventMO eventMO = new EventMO();
            //here messageMO having the details of received message here i am setting eventId from messageMO to eventMO
            // Inorder to passs gropchatActivity
            eventMO.setEventId(messageMO.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, PendingIntent.FLAG_UPDATE_CURRENT);

            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);
            mNotificationManager.notify((int) (long) messageMO.getEventId(), mBuilder.build());

1 个答案:

答案 0 :(得分:0)

你需要做几件事才能达到目的。

      
  1. 将groupchatactivity的启动模式设置为SingleTask
  2.   
  3. 覆盖groupchatactivity中的onNewIntent()方法
  4.   
  5. 在onNewIntent()
  6. 中为新收到的意图调用setIntent(intent)

    在上述答案中酌情反映。