Android 如何使用短信内容提供商?文档在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1976252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:02:18  来源:igfitidea点击:

How to use SMS content provider? Where are the docs?

androidsmsandroid-contentprovider

提问by Mark

I'd like to be able to read the system's SMS content provider. Basically I wanted to make an SMS messaging app, but it would only be useful if I could see past threads etc.

我希望能够读取系统的 SMS 内容提供商。基本上我想做一个短信应用程序,但只有当我能看到过去的线程等时它才会有用。

It seems like there's a content provider for this, but I can't find documentation for it - anyone know where that is?

似乎有一个内容提供者,但我找不到它的文档-有人知道那在哪里吗?

Thanks

谢谢

-------- edit -----------

- - - - 编辑 - - - - - -

Ok I found a way to get the sms inbox provider, and I just dumped all the column names in that provider, looks like this:

好的,我找到了一种获取 sms 收件箱提供程序的方法,我只是转储了该提供程序中的所有列名,如下所示:

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, null,null,null,null); 

// column names for above provider:
0: _id
1: thread_id
2: address
3: person
4: date
5: protocol
6: read   
7: status
8: type
9: reply_path_present
10: subject
11: body
12: service_center
13: locked

I'm just piecing this together from random threads I find around the net, I'm really wondering where this is all documented (if at all)?

我只是从网上找到的随机线程拼凑起来,我真的很想知道这一切都记录在哪里(如果有的话)?

Thanks again

再次感谢

回答by Avtar Guleria

In addition to those u can see the list of fields in sms content provider by using following code:

除了这些,您还可以使用以下代码查看 sms 内容提供程序中的字段列表:

private void displaySmsLog() {
    Uri allMessages = Uri.parse("content://sms/");
     //Cursor cursor = managedQuery(allMessages, null, null, null, null); Both are same
    Cursor cursor = this.getContentResolver().query(allMessages, null,
            null, null, null);

    while (cursor.moveToNext()) {
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            Log.d(cursor.getColumnName(i) + "", cursor.getString(i) + "");
        }
        Log.d("One row finished",
                "**************************************************");
    }

}

回答by Josef Pfleger

Unfortunately the content provider for Sms and Mms (android.providers.Telephony) is not part of the public APIat this moment. Until it is, you can define your own constants using thisas a template.

不幸的是,Sms 和 Mms ( android.providers.Telephony)的内容提供程序目前不是公共 API 的一部分。在此之前,您可以使用作为模板定义自己的常量。

回答by jiahao

This is what I got from API 23:

这是我从 API 23 得到的:

public static final String COLUMN_ID = "_id";
public static final String COLUMN_THREAD_ID = "thread_id";
public static final String COLUMN_ADDRESS = "address";
public static final String COLUMN_PERSON = "person";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_DATE_SENT = "date_sent";
public static final String COLUMN_PROTOCOL = "protocol";
public static final String COLUMN_READ = "read";
public static final String COLUMN_STATUS = "status";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_REPLY_PATH_PRESENT = "reply_path_present";
public static final String COLUMN_SUBJECT = "subject";
public static final String COLUMN_BODY = "body";
public static final String COLUMN_SERVICE_CENTER = "service_center";
public static final String COLUMN_LOCKED = "locked";
public static final String COLUMN_ERROR_CODE = "error_code";
public static final String COLUMN_SEEN = "seen";
public static final String COLUMN_TIMED = "timed";
public static final String COLUMN_DELETED = "deleted";
public static final String COLUMN_SYNC_STATE = "sync_state";
public static final String COLUMN_MARKER = "marker";
public static final String COLUMN_SOURCE = "source";
public static final String COLUMN_BIND_ID = "bind_id";
public static final String COLUMN_MX_STATUS = "mx_status";
public static final String COLUMN_MX_ID = "mx_id";
public static final String COLUMN_OUT_TIME = "out_time";
public static final String COLUMN_ACCOUNT = "account";
public static final String COLUMN_SIM_ID = "sim_id";
public static final String COLUMN_BLOCK_TYPE = "block_type";
public static final String COLUMN_ADVANCED_SEEN = "advanced_seen";
public static final String COLUMN_B2C_TTL = "b2c_ttl";
public static final String COLUMN_B2C_NUMBERS = "b2c_numbers";
public static final String COLUMN_FAKE_CELL_TYPE = "fake_cell_type";
public static final String COLUMN_URL_RISKY_TYPE = "url_risky_type";

And this is how I print all the contents:

这就是我打印所有内容的方式:

    private void readAllMessages() {
    List<Sms> smssList = new ArrayList<Sms>();
    Sms sms;
    Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    if (cursor.moveToFirst()) {
        String message = "";
        do {
            sms = new Sms();
            sms.set_id(cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_ID)));
            sms.setThreadId(cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_THREAD_ID)));
            sms.setAddress(cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_ADDRESS)));
            sms.setPerson((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_PERSON))));
            sms.setDate((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_DATE))));
            sms.setDateSent((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_DATE_SENT))));
            sms.setProtocol((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_PROTOCOL))));
            sms.setRead((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_READ))));
            sms.setStatus((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_STATUS))));
            sms.setType((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_TYPE))));
            sms.setReplyPathPresent((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_REPLY_PATH_PRESENT))));
            sms.setSubject((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SUBJECT))));
            sms.setBody((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_BODY))));
            sms.setServiceCenter((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SERVICE_CENTER))));
            sms.setLocked((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_LOCKED))));
            sms.setErrorCode((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_ERROR_CODE))));
            sms.setSeen((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SEEN))));
            sms.setTimed((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_TIMED))));
            sms.setDeleted((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_DELETED))));
            sms.setSyncState((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SYNC_STATE))));
            sms.setMarker((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_MARKER))));
            sms.setSource((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SOURCE))));
            sms.setBindId((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_BIND_ID))));
            sms.setMxStatus((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_MX_STATUS))));
            sms.setMxId((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_MX_ID))));
            sms.setOutTime((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_OUT_TIME))));
            sms.setAccount((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_ACCOUNT))));
            sms.setSimId((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_SIM_ID))));
            sms.setBlockType((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_BLOCK_TYPE))));
            sms.setAdvancedSeen((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_ADVANCED_SEEN))));
            sms.setB2cTtl((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_B2C_TTL))));
            sms.setB2cNumbers((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_B2C_NUMBERS))));
            sms.setFakeCellType((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_FAKE_CELL_TYPE))));
            sms.setUrlRiskyType((cursor.getString(cursor.getColumnIndexOrThrow(Sms.COLUMN_URL_RISKY_TYPE))));

            Log.v(TAG, "SMS read " + sms);
            smssList.add(sms);
        } while (cursor.moveToNext());
    } else {
        Log.v(TAG, "The user does not have any sms");
    }
}

The source code could be found here: https://github.com/jiahaoliuliu/Akami/tree/feature/allSmsFields

源代码可以在这里找到:https: //github.com/jiahaoliuliu/Akami/tree/feature/allSmsFields

回答by Omar Kam

Use the selectionArgs field

使用 selectionArgs 字段

String limite = "the timestamp converted to String";
Cursor cur = c.getContentResolver().query(uriSMSURI, null,"date" + ">?", new String[] {limite},null);

回答by The EasyLearn Academy

public class main extends Activity {
    /** Called when the activity is first created. */
    String colName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tView = (TextView)findViewById(R.id.txtView);

        ContentResolver cr =getContentResolver();
        Uri uri = Uri.parse("content://sms/inbox");
        //Uri uri = Uri.parse("content://sms"); -- For all SMS
        //Uri uri = Uri.parse("content://sms/sent"); -- For all Sent Items
        //If you want to read the Sent SMS then change the URi to /sent.

        //In this example we are using Query as we have defined URi as above.
        //We have declared all the Column names we need in string array in the second parameter.
        //If you dont need all then leave null
        //Notice that we did not call managedQuery instead we used Query method of ContentResolver
        Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);
        colName = "ColumnName" +"\n";
        colName = colName +  "--------------" + "\n";

        for(int loopCounter=0; loopCounter < messagesCursor.getColumnCount() ; loopCounter++)
        {
            colName = colName + messagesCursor.getColumnName(loopCounter) + "\n";

        }
        colName = colName +  "--------------" + "\n";

        if(messagesCursor.getCount() > 0)
        {
            while(messagesCursor.moveToNext())
            {
                colName = colName +  messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "--";
                colName = colName +  messagesCursor.getString(messagesCursor.getColumnIndex("address")) + "\n";
            }
        }
        tView.setText(colName);


    }
}

回答by CommonsWare

The SMS content provider is undocumented for a reason -- it is not part of the SDK at this time. Please do not use it.

出于某种原因,SMS 内容提供程序没有被记录下来——它目前不是 SDK 的一部分。请不要使用它

回答by Rodrigo Borba

Or you can do something like below:

或者您可以执行以下操作:

for(String s : cursor.getColumnNames()){
     Log.d("smsColumns", "Column: " + s);
}