I am working on a project in which I have made app to default sms app and getting new message body and notification through Broadcast Receiver. It is showing toast that new message has been received and also read new message body.
But problems are
Problem 1:
The newly received sms is not retrieving from my default sms app inbox and not showing in my listview.
Problem 2:
I am not able to get each and every message from each conversation
GetMessage() code:
public ArrayList<String> getSms() {
ContentResolver contentResolver=getContentResolver();
Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
ArrayList<String> messages = new ArrayList<String>();
Cursor cursor = null;
try {
cursor = contentResolver.query(mSmsQueryUri, new String[] { "_id", "address", "date", "body",
"type", "read" }, null, null, "date desc");
if (cursor == null) {
Log.i("curson null", "cursor is null. uri: " + mSmsQueryUri);
Toast.makeText(this, "curor null", Toast.LENGTH_SHORT).show();
}
for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {
String body = cursor.getString(cursor.getColumnIndex("body"));
String address = cursor.getString(cursor.getColumnIndex("address"));
/* String date= cursor.getString(cursor.getColumnIndex("date"));
SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM-DD",Locale.US);
Date datee= dateFormat.parse(date);*/
messages.add("\n"+ "Sender: "+ address+"\n"+"Content: "+body+"\n");
//messages.add(address);
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
} finally {
cursor.close();
}
return messages;
}
MyListner code
public void onReceive(Context context, Intent intent) {
//Getting new Message notification
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle=intent.getExtras();
if(bundle!=null){
Toast.makeText(context, "New Message Received", Toast.LENGTH_SHORT).show();
try {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
message_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
Toast.makeText(context, "Message sent from: " + message_from, Toast.LENGTH_SHORT).show();
if(msgBody.equals("Where are you")){
sendMessage(context);
Toast.makeText(context, "Message Body: " + msgBody, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Other message", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}