ContentObserver Not Working on Api level 17 - android

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();
}

Related

How to listen all outgoing sms in Android (API 20 to 28)

I am developing android app which detect outgoing sms and save into room database. I target new android versions from API 22 to 28.
For incoming sms I am used receiver(RECEIVE_SMS) . But for outgoing sms I try with ContentResolver class. But it is not guaranteed that work. Sometimes not working.
I run it from mainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!hasReadSmsPermission()) {
showRequestPermissionsInfoAlertDialog();
}
ContentResolver contentResolver = getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler(), getApplicationContext()));
SetAlarmPeriodically.schedulePeriodic();
}
and use onbootreceiver to restart ContentResolver
public class OnBootReceiver extends BroadcastReceiver {
#SuppressLint("UnsafeProtectedBroadcastReceiver")
#Override
public void onReceive(Context context, Intent intent) {
JobManager.create(context).addJobCreator(new MyJobCreator());
SetAlarmPeriodically.schedulePeriodic();
}
}
onRunJob
#NonNull
#Override
protected Result onRunJob(#NonNull Job.Params params) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
ContentResolver contentResolver = getContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler(), getContext()));
}
});
return Result.SUCCESS;
}
But it is not worked after reboot phone. How can I listen all outgoing sms everytime after app installed? Any guaranteed solutions?

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 handle Contacts changes in 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());

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);

Catch sent SMS (Android 2.2)

I know that there are a few Questions here on SO relating to this, but none of them helped me to get this working - capture SMS that being sent.
I am using Android 2.2 (FROYO) on a Samsung phone (if that matters somehow).
I've searched a lot for this on Stackoverflow and realized that I need ContentObserver for my request. I'm using Service instead of Activity, so I've registered that ContentObserver in my Service class, and it looks like this:
public class SMSSending extends Service {
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Uri uriSMSURI = Uri.parse("content://sms/sent");
Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);
cur.moveToNext();
String content = cur.getString(cur.getColumnIndex("body"));
Toast.makeText(getApplicationContext(), "SOME TEXT", Toast.LENGTH_LONG).show();
}
#Override
public boolean deliverSelfNotifications() {
return false;
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
MyContentObserver contentObserver = new MyContentObserver();
ContentResolver contentResolver = getBaseContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/sent"),true, contentObserver);
Toast.makeText(getApplicationContext(), "SERVICE CREATED", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(getApplicationContext(), "SERVICE STARTED", Toast.LENGTH_LONG).show();
}
}
As you can see I've put Toast in few places so I could see if this is working at all - and unfortunately none of this notifications appear. Also, i tried with putting some code for LogCat but nothing happens.
I've also tried to put Uri uriSMSURI = Uri.parse("content://sms"); instead of content://sms/sent
but the application simply doesn't do anything.
Of course, I have permissions in Manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
What am i missing?
Fortunately, I've managed to work it out, but by using totally different approach. Maybe is this going to help someone in future..
Instead of using ContentObserver (which I still don't know why didn't work) I've created new Thread and started it after my service has been created and started. So it looks like this:
...
final Uri CONTENT_URI = Uri.parse("content://sms/sent");
...
public void onStart(Intent intent, int startid) {
Go();
}
private void Go(){
new Thread(new Runnable() {
public void run() {
try {
while(true){
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst()){
text = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
if(!text.equalsIgnoreCase(actual)){
previous = text;
//do what you need..
}
}
Thread.sleep(60000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
It's working absolutely stable, even better than with using ContentObserver, having in mind that lot of people had problems with it, something like this and some other..

Categories

Resources