"Received Text" event issue and text related built-in variables issue in MIUI ROM

298 views
Skip to first unread message

David Wang

unread,
Mar 7, 2015, 12:56:31 PM3/7/15
to tas...@googlegroups.com
@Pent,


As with these threads and posts:
...

For a long time, we think that it must be a issue in MIUI ROM when every time the former issues happens in MIUI. In fact, some other apps which I had used, can capture the sms received event and read its content out correctly while only Tasker couldn't work. It's so weird.
Until recently, I found a way to solve these issues/bugs in MIUI. Maybe helpful for other 3rd ROMs which has the same issues.

We can use two methods to monitor sms related events in Android at present. 

One way is to register "BroadcastReceiver" for "android.provider.Telephony.SMS_RECEIVED" event. But for 3rd ROM which maybe had their own message app pre-installed, this may be not work for external apps(e.g. Tasker) to capture the event. Because the built-in message related app may be have a high priority over Tasker(for example, pre-installed Message app in MIUI ROM). These built-in message app once capture the broadcasted messages, they maybe not pass the broadcast to next registered broadcast receiver. (Notice: type of message received broadcast is ordered broadcast. )  I think this way is not so reliable for Tasker.

The other way is to register "ContentObserver" for sms related URIs(e.g. "content://sms/"). Every app(with related sms permissions declared first) can register the URIs, and will be notified when content of these URI changed.  I recommend this way to capture sms related event.

Had known these, I made a small app to monitor sms received event using two former methods. The "BroadcastReceiver" way never worked. But the "ContentObserver" worked, while "Received Text" event in Tasker never worked, too. So I guess maybe Tasker using the "BroadcastReceiver" way or using the "ContentObserver" way with some other URIs.

My test project files attached.(MIUI 4.12.5, Android 4.1.2)



Reference:

MI_20150308_003858.png
SMSReceiveTest.zip

David Wang

unread,
Mar 9, 2015, 5:39:49 AM3/9/15
to tas...@googlegroups.com
The key lines of “ContentObserver” way in my former test project. As follows,

static fields declared:
    public static final String SMS_URI = "content://sms/";
    public static final String SMS_INBOX_URI = "content://sms";

register ContentObserver:
        ContentResolver resolver = getContentResolver();
        resolver.registerContentObserver(Uri.parse(SMS_URI), true,smsContentObserver);

 
    private ContentObserver smsContentObserver =  new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(true);
            
            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(
                    Uri.parse(SMS_INBOX_URI),
                    new String[] { "_id", "address", "thread_id", "date",
                            "protocol", "type", "body", "read" },
                    " read=?", new String[] {"0"},
                    "date desc");
            while(cursor.moveToNext()){
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                String id = cursor.getString(cursor.getColumnIndex("_id"));
                //resolver.delete(Uri.parse("content://sms/"+id), null, null);
                Toast.makeText(getApplicationContext(), address + "," + body, Toast.LENGTH_LONG).show();
                Log.d("ContentObserver---", address + ":::::" + body);
                break;
            }
        }
    };

 
Open the app of test project first, a toast will be shown and related information will be written into system log when sms received on my phone.
The screenshot(I mean the png attachment in my former post) shows the specific lines of system log.

David Wang

unread,
Mar 11, 2015, 1:52:40 AM3/11/15
to tas...@googlegroups.com
@Pent

If current method which Tasker adopt inside "Text Received" event detect works well on most non-MIUI devices, I suggest you check ROM type using former lines first, then adopt special method as my test projects depend the ROM type.

By the way, you can check whether current ROM is MIUI or not using following lines.

// MIUI ROM check 
import java.io.IOException;

public final class MIUIUtils {

private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

public static boolean isMIUI() {
try {
final BuildProperties prop = BuildProperties.newInstance();
return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
} catch (final IOException e) {
return false;
}
}
}


Pent

unread,
Mar 13, 2015, 3:35:58 AM3/13/15
to tas...@googlegroups.com
Prior to KitKat (i.e. on your ROM) Tasker uses the ContentObserver content://mms-sms (need to pick up MMS too).
Perhaps the provider name difference accounts for the problem.

After KitKat, the RECEIVED_TEXT intent became official (and responds much faster) so I switched to that.

Pent

David Wang

unread,
Mar 13, 2015, 12:33:03 PM3/13/15
to tas...@googlegroups.com
Prior to KitKat (i.e. on your ROM) Tasker uses the ContentObserver content://mms-sms (need to pick up MMS too).
Perhaps the provider name difference accounts for the problem.

So weird!

I have test ContentObserver for "content://mms-sms/conversations/", too. It's OK on my phone(MIUI 4.12.5, Android 4.1.2). Some code lines in my test project are taken from tutorial here

Test project files and related screenshot attached. 


MI_20150314_001449.png
SMSReceiveTest-old.zip

David Wang

unread,
Mar 13, 2015, 10:18:01 PM3/13/15
to tas...@googlegroups.com
Forget about this issue.

"Text Received" event in Tasker seems work OK now. I don't know why. Sometimes MIUI make me feel confused and crazy. 

Marta Hintz

unread,
Mar 13, 2015, 10:36:56 PM3/13/15
to tas...@googlegroups.com
Maybe choose a better custom ROM? :-)

Pent

unread,
Mar 16, 2015, 5:19:25 AM3/16/15
to tas...@googlegroups.com

I have test ContentObserver for "content://mms-sms/conversations/", too. It's OK on my phone(MIUI 4.12.5, Android 4.1.2). Some code lines in my test project are taken from tutorial here

I just use mms-sms, no /conversations BTW.

Pent
Reply all
Reply to author
Forward
0 new messages