I have found very strange issue and it seems like below:
The problem is that if I continue messaging with someone then my App will fetch all the messages correctly in sequence, but in between that if there is any interruption like opening the other app or messing with some other stuff in device, then my App stops fetching messages and in that case we need to open the app and It will again start fetching messages but skip the outgoing message that we have sent while messing with other stuff.
For this fetching SMS I am using my Custom SMSContentObserver for fetching SMS. And I am storing the recent SMSID for fetching new messages after that every time.
Please help me here As I can't figure out the reason for this.
Here is the some code snippet :
public class SMSContentObserver extends ContentObserver
{
public SMSContentObserver()
{
super(null);
}
@Override
public void onChange(boolean selfChange)
{
super.onChange(selfChange);
Log.e("", "~~~~~~" + selfChange);
try{
appContactList = new ArrayList<HashMap<String, String>>();
appContactList = databaseHandler.getAllappContact();
if(appContactList.size() != 0){
try{
for (int i = 0; i < appContactList.size(); i++) {
strcontactname = appContactList.get(i).get("app_contact_name");
strcontactnumber = appContactList.get(i).get("app_contact_number");
strcontactid = appContactList.get(i).get("app_contact_id");
strsmsid = appContactList.get(i).get("app_contact_smsid");
strsmsdate = appContactList.get(i).get("app_contact_smsdate");
jsonArray = new JSONArray();
fetchInboxSmsIncoming(1, UserID, strcontactid, strcontactname ,strcontactnumber ,strsmsid ,strsmsdate ,"in");
fetchInboxSmsIncoming(2, UserID, strcontactid,strcontactname , strcontactnumber,strsmsid ,strsmsdate ,"out");
if (Constants.isNetworkAvailable(mContext))
{
new SMSListenerNetwork().execute();
}
else
{
Toast.makeText(PhoneContactListActivity.this, Constants.msgNoInternet, Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean deliverSelfNotifications()
{
return true;
}
}
fetchInboxSmsIncoming method :
public void fetchInboxSmsIncoming(int type, String userID2, String strcontactid2,
String strcontactname2, String strcontactnumber2, String strsmsid2,
String strsmsdate2, String direction) {
// TODO Auto-generated method stub
try{
JSONObject obj = null;
databaseHandler = new DatabaseHandler(mContext);
Uri uriSms = Uri.parse("content://sms");
Cursor cursor = this.getContentResolver()
.query(uriSms,
new String[] { "_id", "address", "date", "body",
"type", "read" }, "type=" + type , null,
"date" + " COLLATE LOCALIZED ASC");
if (cursor != null) {
cursor.moveToLast();
int i = 0;
if (cursor.getCount() > 0) {
do {
Log.e("message.messageNumber",cursor.getString(cursor
.getColumnIndex("address")));
String incomingmessagefromSMS = processWord(cursor.getString(cursor.getColumnIndex("address")));
String incomingmessagefromPhone = processWord(strcontactnumber2);
if((incomingmessagefromSMS.contains(incomingmessagefromPhone)) && Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id"))) > Integer.parseInt(strsmsid2)){
obj = new JSONObject();
//Toast.makeText(mContext, ""+incomingmessagefromSMS+" = " +incomingmessagefromPhone, Toast.LENGTH_LONG).show();
try {
String _id = cursor.getString(cursor.getColumnIndex("_id"));
String date = cursor.getString(cursor.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
obj.put("Date", formatter.format(calendar.getTime()));
String text = cursor.getString(cursor.getColumnIndexOrThrow("body"));
if(text.contains("\""))
text = text.replace("\"", "");
obj.put("Text", text);
obj.put("UserID", userID2);
obj.put("ContactID",strcontactid2);
obj.put("Direction",direction);
if(i == 0){
databaseHandler.insertappContact(strcontactid2, strcontactname2, strcontactnumber2, _id, formatter.format(calendar.getTime()));
i = 1;
}
//Toast.makeText(mContext, "SMS"+obj, Toast.LENGTH_LONG).show();
jsonArray.put(obj);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
}
}
if(!cursor.getString(cursor
.getColumnIndex("address")).contains(strcontactnumber2)){
break;
}
} while (cursor.moveToPrevious());
}
}
if(type == 1){
Log.e("jsonArray 1 ",""+jsonArray);
}else{
Log.e("jsonArray 2",""+jsonArray);
}
}catch(Exception e){
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
}
}
SMSListenerNetwork Async Task :
public class SMSListenerNetwork extends AsyncTask{
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
try{
String url = Constants.API_URL + "MessageConversation";
List<NameValuePair> Parameters = new ArrayList<NameValuePair>();
JSONObject obj = new JSONObject();
String json = null;
try {
json = obj.put("users", jsonArray).toString();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Parameters.add(new BasicNameValuePair("ConversationText",json ));
Parameters.add(new BasicNameValuePair("DeviceID", androidToken));
Parameters.add(new BasicNameValuePair("Token", securitytoken));
ServerResponse response = jsonParser.postData(Parameters, url, Constants.API_RESPONSE_TYPE_JSON_OBJECT);
Log.e("Parameters",""+Parameters);
Log.e("url",""+url);
System.out.println("Responce:"+response);
Log.e("Responce",""+response);
if(response.getStatus() == Constants.RESPONSE_STATUS_CODE_SUCCESS){
JSONObject jsonObj = response.getjObj();
System.out.println("jsonObjEmp:"+jsonObj);
try {
response_message = jsonObj.getString("msg");
String status = jsonObj.getString("status");
if(status.equals("Success") || response_message.equals("Data is missing"))
{
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result)
{
try {
if(jsonArray.isNull(0)){
Log.e("SMSRECEIVER", "SMS response_message "+"Data is missing");
}else{
Log.e("SMSRECEIVER", response_message);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("SMSRECEIVER", response_message);
}
}
}
Please provide some suggestions here.
Thanks in advance.