Android:使用 ContentObserver 捕获传出短信或接收器不起作用

2023-12-02

我试图使用内容观察器捕获传出的短信事件。

//TEST OBSERVER
        ContentObserver co = new SMSoutObserver(new Handler(), getApplicationContext());
        ContentResolver contentResolver = getApplicationContext().getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, co);
        // END TEST OBSERVER

and

public class SMSoutObserver extends ContentObserver {

    private Context mCtx;

    public SMSoutObserver(Handler handler, Context ctx) {
        super(handler);
        mCtx = ctx;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        // save the message to the SD card here
        Logger.d("On Change");
        Toast.makeText(mCtx,"TEST", Toast.LENGTH_LONG).show();
    }
}

但是,如果我在模拟器中发送传出消息,则不会触发事件。

我也尝试过使用接收器。

<receiver android:name=".receiver.SMSReceiver"
                  android:enabled="true"
                  android:exported="true"
                  android:priority="1000">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.provider.Telephony.SMS_SENT"/>
            </intent-filter>
        </receiver>

带接收器

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {

            Logger.d("SMSReceiver triggered");
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
                //do something with the received sms
                Logger.d("Incoming SMS");
            }else  if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
                //do something with the sended sms
                Logger.d("Outgoing SMS");
            }
            // Start reminder service
            // context.startService(new Intent(context, ReminderService.class));
        } catch (Exception e) {
            Logger.e("onReceive method cannot be processed");
            TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
                    Constants.ExceptionMessage.EXC_CANNOT_CANNOT_PROCESS_REBOOT_RECEIVER, true);
        }
    }

}

但该接收器仅针对传入消息触发,而不针对传出消息触发。我应该如何以正确的方式在 Android 版本 > 5 上进行操作。

非常感谢您的任何建议


没有"android.provider.Telephony.SMS_SENT"SDK 中的操作,并且 SMS 发送时也没有任何系统范围的广播,因此您的第二种方法将不起作用。

至于ContentObserver, the Uri您注册的必须是基地Uri对于短信提供商。也就是说,改变Uri.parse("content://sms/out") to Uri.parse("content://sms")。如果您只想处理传出消息,则必须在onChange()方法,并检索type消息的列值,检查值2,表示已发送消息。

如果您支持低于 16 的 API 级别,那么它会是这样的:

private static final Uri uri = Uri.parse("content://sms");  
private static final String COLUMN_TYPE = "type";
private static final int MESSAGE_TYPE_SENT = 2;
...

@Override
public void onChange(boolean selfChange) {
    Cursor cursor = null;

    try {
        cursor = resolver.query(uri, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int type = cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE));

            if (type == MESSAGE_TYPE_SENT) {
                // Sent message
            }
        }
    }
    finally {
        if (cursor != null)
            cursor.close();
    }
}

从 API 16 开始,ContentObserver课程提供了onChange()重载将提供特定的Uri将消息作为第二个参数,您可以比基本参数更有效地查询和检查Uri。此外,如果您的最低 API 级别为 19,则有几个带有常量的类可以替换上面示例代码中定义的常量。

作为旁注,Uri发件箱是"content://sms/outbox", not "content://sms/out".

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:使用 ContentObserver 捕获传出短信或接收器不起作用 的相关文章

随机推荐