如何接收短信

时间:2013-11-27 08:32:14

标签: java android

我需要在android上编写应用程序,将INBOX中的所有消息都放到我的应用程序中。

我的代码是正确的,但是显示了发件人的数量,但如果发件人保存在我的手机中,我想要发件人的姓名

此代码:

public class SecureMessagesActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setTheme( android.R.style.Theme_Light );
        setContentView(R.layout.main);

        /**
         * You can also register your intent filter here.
         * And here is example how to do this.
         *
         * IntentFilter filter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED" );
         * filter.setPriority( IntentFilter.SYSTEM_HIGH_PRIORITY );
         * registerReceiver( new SmsReceiver(), filter );
        **/
        onClick() ;

    }

    ArrayList<String> smsList = new ArrayList<String>();



    public void onClick() 
    {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);

        int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
        int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );

        if ( indexBody < 0 || !cursor.moveToFirst() ) return;

        smsList.clear();

        do
        {

            String str = cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
            smsList.add( str );
        }
        while( cursor.moveToNext() );


        ListView smsListView = (ListView) findViewById( R.id.SMSList );
        smsListView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, smsList) );


}
} 

1 个答案:

答案 0 :(得分:1)

使用以下方法获取所有已保存的联系人:

public static ArrayList<ContactWrapper> fillContacts(Context c) {
    ArrayList<ContactWrapper> contacts = new ArrayList<ContactWrapper>();
    try {

        // private Uri uriContact;

        String contactName = null;
        String contactID = null;
        String contactNumber = null;

        ContactWrapper cWrapper;

        // getting contacts ID
        Cursor cursorID = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);

        if (cursorID.moveToFirst()) {
            do {
                contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
                contactName = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                contactNumber = DataUtil.retrieveContactNumber(c, contactID);

                if (contactNumber != null && (!contactNumber.equalsIgnoreCase("")) && (!contactNumber.equalsIgnoreCase(" "))) {
                    cWrapper = new ContactWrapper(contactID, contactName, contactNumber, 0);
                    contacts.add(cWrapper);

                    Log.d(c.getClass().getSimpleName(), "Contact ID: " + contactID);
                }

            } while (cursorID.moveToNext());
        }
        cursorID.close();

    } catch (Exception e) {
        e.printStackTrace();
        Log.e(c.getClass().getSimpleName(), "" + e.getMessage());
    }
    return contacts;
}

在你的方法中从短信获取号码并在上面的列表中找到号码(如果存在),然后显示如下名称:

private boolean contactExistInPhoneList(String number) {
    for (int i = 0; i < phoneListcontacts.size(); i++) {
        if (phoneListcontacts.get(i).getNumber().equalsIgnoreCase(number)) {
            Log.d(this.getClass().getSimpleName(), "Name= "+phoneListcontacts.get(i).getName());
            return true;
        }
    }
    return false;
}

希望它有所帮助。如果有帮助,请将其标记为真或投票。