How to handle Contacts changes in Android? - android

I want to listen for Contacts changes. How do I do that?

I've done it.
protected class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
text.append("created at + " + new GregorianCalendar().getTime() + "\n");
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
text.append("Changed\n");
}
}
and also we need to register ContentObserver:
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(Contacts.CONTENT_URI, true,
new MyContentObserver());

Related

ContentObserver Not Working on Api level 17

I want to detect change when contact get modified/added and deleted. So My Code is working fine on api level 23 and and api 22 but it is not working on api level 17.So please help me on this.
Here is My Content Observer Code.
public class MyContentObserver extends ContentObserver {
public MyContentObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
Log.d("Change", "Yes");
}
}
and Code to Register Content Observer:
myContentObserver = new MyContentObserver(new Handler());
try {
getApplicationContext().getContentResolver()
.registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true,
myContentObserver);
} catch (Exception e) {
e.printStackTrace();
}

How to track sent messages (SMS) in android?

I am developing an Android application which has one of it's features as to detect any messages (SMS) sent by an app to any number. For this I want to keep track of sent messages. I want to check the message as and when it is sent. I know there is a Broadcast Receiver for SMS Recieved which will automatically call the function when a message is received. Is there any method to do that for SMS Sent? I just need a technique to do that.
Create a ContentObserver
public class InboxContentObserver extends ContentObserver {
ContentResolver cr;
Context ctxt;
public InboxContentObserver(Handler handler, Context ctxt) {
super(handler);
this.cr = ctxt.getContentResolver();
this.ctxt = ctxt;
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i(TAG, "Starting sms sync.");
new Thread(new Runnable() {
#Override
public void run() {
Cursor sms_sent_cursor = cr.query(Uri
.parse("content://sms"), null, "date > ?",
new String[]{LAST_SYNC_TIME}, "_id asc");
//....write your code here
}
}).start();
}
}
Replace LAST_SYNC_TIME with the time.
Register the ContentObserver
new InboxContentObserver(new Handler(), getApplicationContext());
getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, inboxContentObserver);

ContentObserver's Onchange method is not being called while sending an sms

I have defined the following service with an observer of sent messages. The problem is that when sending a message, onChange() method is not getting called, anyone please tell me why?
thanks
public class countService extends Service {
ContentResolver contentResolver;
MyContentObserver Observer;
Uri sms_content = Uri.parse("content://sms/sent");
public Cursor cursor;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
contentResolver = getBaseContext().getContentResolver();
Observer = new MyContentObserver();
contentResolver.registerContentObserver(sms_content, true, Observer);
super.onCreate();
}
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("BOOOOOOOOOOOOOOOOOO", "c");
Cursor cursor = contentResolver.query(sms_content, null, null,
null, null);
cursor.moveToFirst();
String type = cursor.getString(cursor.getColumnIndex("type"));
Log.d("THEMESSAGE", type);
}
}
}
For registering ContentObserver for send / received SMS you will need to register ContentObserver for "content://sms" instead of "content://sms/sent" and put your logic for getting incoming or outgoing sms inside onChange method of ContentObserver:
Change your service onCreate() as:
#Override
public void onCreate() {
// TODO Auto-generated method stub
contentResolver = getBaseContext().getContentResolver();
Observer = new MyContentObserver();
contentResolver.registerContentObserver(
Uri.parse("content://sms"),true, Observer);
super.onCreate();
}
and in MyContentObserver ContentObserver you can check for send/received SMS and make sure you have add SMS READ Permission in Manifast :
<uses-permission android:name="android.permission.READ_SMS"/>

How to check which ContentObserver triggers the onChange method?

What I want to do is to have a Service with more than one ContentObserver registered and check which ContentObserver triggers onChange() to do specific reactions. I dont know if I just have to include a if/else inside the onchange(), or Overwrite it to each ContentObserver, in either cases I wouldnt know exactly how to do it. Thanks in advance for any help.
public class SmsObserverService extends Service {
private String BABAS = "babas";
#Override
public void onCreate() {
Handler handler = new Handler();
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, new SmsObserver(handler));
//Second observer
this.getContentResolver().registerContentObserver(Uri.parse("CallLog.Calls.CONTENT_URI"), true, new SmsObserver(handler));
}
#Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}
public class SmsObserver extends ContentObserver{
public SmsObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
//Where I should check somehow which ContentObserver triggers the onChange
//This code to the sms log stuff, the call log part will be included
//when I find out how to check whic observer trigerred the onChange
Uri uri = Uri.parse("content://sms/");
Cursor cursor = getApplicationContext().getContentResolver().query( uri, null, null, null, null);
cursor.moveToNext();
String body = cursor.getString(cursor.getColumnIndex("body"));
String add = cursor.getString(cursor.getColumnIndex("address"));
String time = cursor.getString(cursor.getColumnIndex("date"));
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
if(protocol == null){
Toast.makeText(getApplicationContext(), "Enviada para: " +add + ", Hora: "+time +" - "+body, Toast.LENGTH_SHORT).show();
Log.i(BABAS, "Enviada para: "+add +" " +"Time: "+time +" - "+body);
}else{
Toast.makeText(getApplicationContext(), "Recebida de: "+add + ", Hora: "+time +" - "+body, Toast.LENGTH_SHORT).show();
Log.i(BABAS, "Recebida de: "+add +" " +"Time: "+time +" - "+body);
}
}
}
}
I'd simply extend ContentObserver and add whatever I am missing there.
mObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
#Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
}
}
};
You can check uri with above method

Receive broadcast when send an sms

How to receive broadcast when a user sends SMS from his Android phone? I am creating an application which is taking track of sent SMS and calls. I am done with the calls part, please help me with the SMS. Note that sms are sent by the phone not any application.
----------//solution-----------
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(VIEW_RESOURCE_ID);
SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
}
public class SendSmsObserver extends ContentObserver {
public SendSmsObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
Log.d("sent sms", "one text send");
}
}
I found the answer
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(VIEW_RESOURCE_ID);
SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
}
public class SendSmsObserver extends ContentObserver {
public SendSmsObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
Log.d("sent sms", "one text send");
}
}
You could build on CallLog. The CallLog provider contains information about placed and received calls.
The Following code can work
Cursor c = null; try {
c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if (c != null && c.moveToFirst()) {
do {
int duration = c.getInt(c.getColumnIndex(CallLog.Calls.DURATION));
// do something with duration
} while (c.moveToNext());
} } finally {
if (c != null) {
c.close();
} }
--------------------------ADDED NEW SOLUTION------------------------
Have a look at:
http://groups.google.com/group/android-developers/browse_thread/thread/9bc7d7ba0229a1d2
and :
http://code.google.com/p/android/issues/detail?id=914
Basically, you can do it by registering a content observer on the SMS
message store.Try
this:
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);

Categories

Resources