How Can I Read Unread Sms From Inbox In Android Programatically?
Hi, I am new for android application development, and I have developed one simple application in android. In my application I have to add a new feature. I want to read sms messages
Solution 1:
Its Really Work... save with Message.java
package mzsay.com.provider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class Message {
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
@SuppressWarnings("unused")
private ContentResolver resolver;
public Message(ContentResolver ConResolver){
resolver = ConResolver;
}
public String getMessage(int batas) {
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
int hitung = 0;
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
if(hitung == batas)
break;
hitung++;
}
return sms;
}
public int getMessageCountUnread(){
Cursor c = resolver.query(SMS_INBOX, null, "read = 0", null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();
return unreadMessagesCount;
}
public String getMessageAll(){
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
}
return sms;
}
public String getMessageUnread() {
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
int hitung = 0;
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
if(hitung == getMessageCountUnread())
break;
hitung++;
}
return sms;
}
public void setMessageStatusRead() {
ContentValues values = new ContentValues();
values.put("read",true);
resolver.update(SMS_INBOX,values, "_id="+SmsMessageId, null);
}
}
And than call with this method in your Main Activity
public synchronized String getMessage(int batas) {
Message ambilpesan = new Message(context.getContentResolver());
//this a return of All unread from your Inbox
return ambilpesan.getMessageUnread();
}
Solution 2:
If you are extending DroidGap, no need to do setContentView in onCreate, DroidGap will display webView by default.
Create a Class say ReadSMS having method to read unread sms and add ReadSMS class object as a javascriptInterfcae to droidGap webview, as below:
Class ReadSMS{
public ReadSMS(){
}
public String[] readUnreadSMS(){
return smsArray;
}
}
In SMSActivity's onCreate add:
ReadSMS readSms = new ReadSMS();
appView.addJavascriptInterface(readSms , "ReadUnreadSMS");
super.loadUrl("file:///android_asset/www/index.html");
Then in Javascript, you can access readUnreadSMS() in ReadSMS class as below:
<script>
$(function(){
var smsArray[] = window.ReadUnreadSMS.readUnreadSMS();
});
</script>
Post a Comment for "How Can I Read Unread Sms From Inbox In Android Programatically?"