I wanna make an android app that runs in background and check if there is a miscall or new massage and if it is true do something.How can I run my app in background (it does not close when I click back button) and how to access miscall and new message state I am an amateur in android.
Thanks!
For not seen sms messages there is a column in content://sms that you can check in order to determine if a message has been seen.
Column name is "seen".
example of calling it and checking:
ContentResolver mContectResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
String[] mProjection = {
"_id",
"address",
"person",
"date",
"body",
"protocol",
"seen"
};
Cursor cursor = mContectResolver.query(uri, mProjection, null, null, null);
cursor.moveToFirst();
for(int i = 0 ; i<cursor.getCount() ; i++){
if(cursor.getString(cursor.getColumnIndexOrThrow("seen")).equalsIgnoreCase("1")){
//Message has not been seen
}
cursor.moveToNext();
}
cursor.close();
Related
My app is currently displaying all the SMS in the inbox with this code:
public void onClick(View v) {
if (v == btnInbox) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
}
and I want to display only messages from one specific sender, what will i change to eliminate messages from the other sender?
apply selection arguments to filter the contents based on address field.
Actual code becomes:
String[] selectionArgs = { "12672631" }; //add the phone number here
Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);
Im building a app that is reading last sms from spec number (vb# 8888). Code is working great. I only have one prob. When i get a new sms from other number (vb# 7777)my code stops reading sms from (v#8888). if i delete the new sms from (7777) than my code strats working again. I'm using this to update a string in my app. Can someone help me
This is my code
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri, null, null, null, null);
String[] columns = new String[]{"address", "body"};
if(cursor1.moveToFirst()) {
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if address.equalsIgnoreCase("+597*******")) {
body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);//body
Your code is just reading the most recent message in the inbox, whomever it's from. If you want the most recent message from that particular number, you can adjust your query to match the number, and limit the results to one record.
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
String phoneNumber = "+597*******";
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection,
"address = ?",
new String[] {phoneNumber},
"date DESC LIMIT 1");
if (cursor1 != null && cursor1.moveToFirst()) {
body = cursor1.getString(cursor1.getColumnIndex("body"));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);
}
Is there a way to tell if a phone number has an sms(s) in android ??
I want to know if there is a way to tell using the sms content porvider I know its not documented and not recommanded to use but is there a way to tell ??
what i have tried .
this statement will get all the inbox sms(s)
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), null, null, null,
null);
I cant use the where clause like this
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), "address =?", new String[]{"001435436654747"}, null,
null);
because the number may be stored without the global code the "001" or such .
is there a work around for this ??
Hi you can use below code to do this , I use this in my application to listen incoming sms to reply automaticaly.
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(myMessage, new String[] {
"_id", "address", "date", "body",
"read" }, null, null, null);
System.out.println(Number);
while (c.moveToNext()) {
Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
if (Number.equals("001435436654747")) {
// DO YOUR STUFF
}
}
I am new to Android. I want to know how to search by word in SMS inbox within the application. Can anyone help me. Please give me the source code if you could.
** Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox.**
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveToFirst();
do{
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
if(msgData.contains(yourWord))
;// do something
}while(cursor.moveToNext());
This seems to have been answered several times already here and there for pointer for instance.
Please do search for existing questions before creating new ones.
first you have to query the inbox content provider for sms
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI,new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null,null);
Then search iterate and get sms body
if(cur.getCount()>0) {
while(cur.moveToNext()) {
String smsBody = cur.getString(5);
Log.v("body",smsBody);
if(smsBody.contains("matching string")) {
}
also refer this link
how can i read inbox in android mobile?
I want to display a list of the contact names from the SMS inbox and outbox (like in the native messaging app). I have come up with the following code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex(columns[0]));
sms += address + " ";
String contact=address;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
startManagingCursor(cs);
if(cs.getCount()>0)
{
cs.moveToFirst();
contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
listItems.add(contact);
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
This works when I run the application in an emulator, but when I try to run it on a phone I get a NullPointerException. If I double-click on the error message in the LogCat the following line gets highlighted:
if(cs.getCount()>0)
What is the problem here?
From the documentation for ContentResolver.query(...)
A Cursor object, which is positioned before the first entry, or null
You must always check for null when using a ContentResolver to query for data.
Also from the same documentation.
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
So instead of:
Cursor cs= getContentResolver().query(uri, new String[ {PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
Do:
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"=?",new String[]{address},null);
My first guess would be that cs is null. As to why it is null, I suppose that the carrier/manufacturer could have changed how this is implemented on your device. Without knowing too much about how these systems work, it looks like you are querying against some kind of database and then using a cursor to iterate over the data. If the representation has been changed, the cursor will break. My suggestion would be to see if there is some other solution that is more Google/API driven (as opposed to reading the raw data).