Android Question Is there something similar to ContentObserver in B4A?

bsnqt

Active Member
Licensed User
Longtime User
Sorry my question is simple, is there something similar to ContentObserver in B4A? Where I can find this library, it seems that it is not in ContentResolver. Thank you very much.
 

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel, thanks for your reply. I am working with MMS interception.
I am trying to monitor the change of MMS inbox, something like this:

B4X:
  public void startMMSMonitoring() {
      try {
        monitorStatus = false;
        if (!monitorStatus) {
            contentResolver.registerContentObserver(Uri.parse("content://mms-sms"), true, mmsObserver);
 
            Uri uriMMSURI = Uri.parse("content://mms");
            Cursor mmsCur = mainActivity.getContentResolver().query(uriMMSURI, null, "msg_box = 4", null, "_id");
            if (mmsCur != null && mmsCur.getCount() > 0) {
              mmsCount = mmsCur.getCount();
              Log("", "MMSMonitor :: Init MMSCount ==" + mmsCount);
            }
        }
      } catch (Exception e) {
        Log("", "MMSMonitor :: startMMSMonitoring Exception== "+ e.getMessage());
      }
  }

As far as people said BroadcastReceiver cannot be used to monitor (or intercept) the MMS, so using ContentObserver is one way we can do it.
I see in B4A Wish forum, some members also want to have this ContentObserver.
With ContentResolver I can do the second part of the codes above in B4A but cannot do the first part with ContentObserver.

Thx again
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a somewhat similar code in SmsInterceptor that allows intercepting SMS messages.

The main problem with such a solution is that it will only work while the process is running (unlike a static filter). So unless your program is running all the time it will not be really useful.

It shouldn't be difficult to take the above code and compile it to a library with SLC tool.
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Thank you. Yes I also think about compiling the code into library, but I do it normally only when I tried all the possible methods in B4A but could not find out the solution.

There is a somewhat similar code in SmsInterceptor that allows intercepting SMS messages.

Do you refer to some special code that is in B4A SmsInterceptor library or you mention in generally? If you refer to B4A library can you point me a link, I would like to refer to learn. If you mention it just generally then I will try to googling around. Appreciate.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SmsInterceptor.ListenToOutgoingMessages():
B4X:
public void ListenToOutgoingMessages() {
       final Uri content = Uri.parse("content://sms");
       ContentObserver co = new ContentObserver(new Handler()) {
         @Override
        public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Cursor cursor = BA.applicationContext.getContentResolver().query(
               content,null, null, null, null);
           if (cursor.moveToNext()) {
             String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
             int type = cursor.getInt(cursor.getColumnIndex("type"));
             if (protocol != null || type != 2) {
               return;
             }
          ba.raiseEvent(null, eventName + "_messagesent", cursor.getInt(cursor.getColumnIndex(BaseColumns._ID)));
             cursor.close();
           }
        }
       };
       ContentResolver contentResolver = BA.applicationContext.getContentResolver();
       contentResolver.registerContentObserver(content, true, co);
     }
 
Upvote 0
Top