简体   繁体   English

如何接收短信

[英]How to receive sms

I need to write application on android to put all message in INBOX to my application . 我需要在android上编写应用程序,以将INBOX中的所有消息都放到我的应用程序中。

my code is correct,but appear number of sender ,but i want name of the sender if the sender save in my phone 我的代码是正确的,但是出现了发件人的号码,但是如果发件人保存在我的手机中,则我想要发件人的名字

this code : 此代码:

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


}
} 

Use below method to get all saved contacs: 使用下面的方法来获取所有保存的内容:

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

And in your method get number from sms and find number in above list if exist then show the name like below : 并在您的方法中从短信中获取号码并在上面的列表中查找号码(如果存在),然后显示如下名称:

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

Hope it helps. 希望能帮助到你。 If it helps you please marked it true or up vote it. 如果有帮助,请标记为“真”或投上一票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM