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

后端 未结 7 1428
耶瑟儿~
耶瑟儿~ 2020-11-29 19:32

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

7条回答
  •  隐瞒了意图╮
    2020-11-29 20:06

    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);
    
    
        }
    }
    

提交回复
热议问题