I am developing a chat application. In that if i am online, sending and receiving messages with current date and time is fine. But if i am offline again coming to online after 2 hours, i am receiving messages with current time itself, but i want to receive the message time with sender time. help me out of this.
Thanks in advance.
send part
if (connection != null) {
connection.sendPacket(msg);
data = new Msg();
data.setMessage(text);
data.setSenderName(userFrom);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(true);
MessageListAdapter.messagesItems.add(data);
notifyMyAdapter();
}
receive part
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
#Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
String from = to + "#localhost/Smack";
if (from.equalsIgnoreCase(message.getFrom())) {
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Msg data = new Msg();
String subject = message.getSubject();
if (subject == null) {
String text = message.getBody();
data.setMessage(text);
data.setSenderName(NAME);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
notifyMyAdapter();
}
});
}
}
}
}, filter);
}
I think all you have to do is while publishing the message data.setCreatedAtTime(System.currentTimeMillis();
the good thing about System.currentTimeMillis(); is that it is in epoch and millisecondsformat and would remain constant over all timeZones.
now on the reciever side you can convert the milliseconds into which ever format you desire
Related
I enabled mam archiving in ejabberd , when I send a messsage to users ,
the receiver of message has information about timestamp of message in received packet like bellow
<archived by='989138553343#192.168.43.67' id='1508653008093085'
xmlns='urn:xmpp:mam:tmp'/>
is it pussible to get this info in StanzaIdAcknowledgedListener that I am using it like bellow :
(I am using ejabbeejabberd-17.08 server and smack in android client .)
mConnection.addStanzaIdAcknowledgedListener(stanzaId, new
StanzaListener() {
#Override
public void processStanza(final Stanza packet) throws
SmackException.NotConnectedException, InterruptedException {
AndroidUtilities.runOnUIThread(new Runnable() {
#Override
public void run() {
//handle ack received by server
}
});
}
});
Yes, it is possible using DelayInformation message extension if you have got it in message stanza.use below namespace and elementname for get id and timestamp.
Message msg = (Message) stanza; //your stanza packet
if(msg.getExtension(DeliveryReceipt.ELEMENT,DeliveryReceipt.NAMESPACE) !=null){
DelayInformation timestamp = (DelayInformation)msg.getExtension("delay", "urn:xmpp:delay");
if (timestamp == null)
timestamp = (DelayInformation)msg.getExtension("x", "jabber:x:delay");
if (timestamp != null)
ts = timestamp.getStamp().getTime();
else
ts = System.currentTimeMillis();
}
So I have this pretty simple app. It sends encrypted SMS messages to the specified phone number. It works swell, but I am having issues finding a way to make the recieved messages automatically show up in the message log. I currectly have a "refresh" button that updates the message log if a new message is available. I don't want to have to use a refresh button, I want the message to simply show up as it is received.
The way the app works is, it takes the message to be sent from the textbox and encrypts it. It then sends the message and when received, it decrypts and stores in a variable and presents in the message log (after I press "refresh").
I have tried many searches on google but can not find something useful because the words are too sensetive. I usually find links to people not being able to receive messages or just links to download messaging apps.
Here is some of my code. This is the receive sms part.
public class SmsReceiver extends BroadcastReceiver
{
public static String decrypted = "";
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str ="";
String info = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
info += "SMS from " + msgs[i].getOriginatingAddress();
info += " : ";
str += msgs[i].getMessageBody().toString();
if (i==msgs.length-1 && MainActivity.locked == true){
try {
decrypted = AESHelper.decrypt(MainActivity.seed, str);
} catch (Exception e) {
e.printStackTrace();
}
MainActivity.rand = Math.random();
MainActivity.seed = String.valueOf(MainActivity.rand);
decrypted = info + decrypted;
info = "";
MainActivity.locked = false;
}
}
}
So, in my main activity, I have the refresh button set to check the length of decrypted. If length of decrypted > 0 then I take decrypted's content and display them in the message log.
There are multiple ways to do this. Simple way would be to use TimerTask that runs every second in your MainActivity to check the length of 'decrypted' variable. Timertask does not run on ui thread. But since you are using it only to check the length of a variable you should be fine. If you need help with timertask, use my example below:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new SpecificTask(), 1000, 200);
But you have to define your SpecificTask() class...
private Specific Task extends TimerTask{
#Override
public void run() {
if(decrypted.length()>0){
//do refresh here but make sure you run this code onuithread
//just use runonuithread...
}
}
}
Or you could just use a handler object...
boolean mStopHandler = false;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
if (!mStopHandler) {
if(decrypted.length()>0){
//refreshlayout code
}
handler.postDelayed(this, 500);
}
}
};
// start your handler with:
handler.post(runnable);
This is my code:
public class MMSReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String incomingNumber = null;
if(intent.getAction().equals("android.provider.Telephony.WAP_PUSH_RECEIVED"))
{
Bundle bundle = intent.getExtras();
try
{
if (bundle != null)
{
String type = intent.getType();
if(type.trim().equalsIgnoreCase("application/vnd.wap.mms-message"))
{
byte[] buffer = bundle.getByteArray("data");
incomingNumber = new String(buffer);
int indx = incomingNumber.indexOf("/TYPE");
if(indx>0 && (indx-15)>0)
{
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
indx = incomingNumber.indexOf("+");
if(indx>0)
{
incomingNumber = incomingNumber.substring(indx);
System.out.println("Mobile Number: " + incomingNumber);
}
}
}
I have bundle data with me when sms arrives to my device .Then can i able to send broadcast with this bundle data to default sms application
If you getting a sms broadcast event, you don't need to send from your side. As the same broadcast will be received by all applications watching for that intent.
Now unless, you want to change something. then you need to cancel the broadcast and make your own broadcast and fire it. and yes you can do that. make sure, your receiver priority is high enough to receive event before other apps do.
Hope this is what you asking.
I am working on a sms application. I am able to send a message from my application using the function sendMessage("subject" , "to");. I need to know how to receive the message sent from the other end. I need to get that data and display the data in textview in my application. How can i achive that. How can i achive that. Please guide me.
Thanks in aadvance.
this will help you:
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
// Listener for incoming message from any user
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
final Message message = (Message) packet;
if (message.getBody() != null) {
fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
}
}
});
}
}
}, filter);
I use asmack building a jabber client for android, it works well ,but I got a problem when sending a large text in Chinese. The XMPPConnection just disconnected from server immediately,here are my Codes of Connection initialization:
ConnectionConfiguration c = new ConnectionConfiguration(mServerAddress, mPort);
c.setSecurityMode(SecurityMode.disabled);
c.setReconnectionAllowed(true);
c.setCompressionEnabled(false);
mConn = new XMPPConnection(c);
and I send a chat message like this:
final Message msg = new Message(chatMsg.getTo(), Message.Type.chat);
msg.setBody(chatMsg.getBody());
new Thread() {
#Override
public void run() {
if(mConn != null && mConn.getUser() != null) {
mConn.sendPacket(msg);
}
}
}.start();