开发者

Access message inbox without Content UrI (content//:sms)

开发者 https://www.devze.com 2023-01-03 07:49 出处:网络
I want to access the messages stored in inbox th开发者_如何学Pythonrough my android project. I have tried the method of forming URI for sms (content://sms/

I want to access the messages stored in inbox th开发者_如何学Pythonrough my android project. I have tried the method of forming URI for sms (content://sms/ inbox) and then quering it for various parametres.

But I cannot find any documentation for reading inbox sms in the standard Android Developr Docs. It was mentioned at various websites that this content has been removed from the standard sdk. The application may not support higher version of android.

So, how can I create an apllication to read sms from inbox that is reliable in furture version of android.

Please help !!


it works well in my milestone (sdk update 2.1)

 public List<String> getSms() {
        Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
        List<String> messages = new ArrayList<String>();
        Cursor cursor = null;
        try {
            cursor = mContentResolver.query(mSmsQueryUri, null, null, null, null);
            if (cursor == null) {
                Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
                return messages;
            }

            for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {
                final String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                messages.add(body);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        } finally {
            cursor.close();
        }
        return messages;
    }

Please make sure you have the read sms permission:

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>


There is a standard SmsMessage use to send messages. http://developer.android.com/reference/android/telephony/SmsManager.html

However, I think the Content Provider solution is more stable than Concrete Class, because Content Provider is the abstract layer to access data in cross-application case. In long-time view, everything could be changed. To face it, developers will benefit from adopting design pattern, a wrapper class to manipulate the content provider (Facade), or use Data Access Object pattern, etc.

In android configuration file, we can limit sdk level to prevent from something lost:

<uses-sdk 
    android:minSdkVersion="5" 
    android:maxSdkVersion="8"
    android:targetSdkVersion="7" />

I'm a newbie in android development, even if I have read many documents or books, I know there are a lot of black magic in the source code. The change is too fast to write the complete document (keeps it up to date is impossible), so don't worry about the AUTHORITY or Class changed.

my 2 cents

0

精彩评论

暂无评论...
验证码 换一张
取 消