Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show SMS in Device

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 148
    Comment on it

    If you want to create a function to Show SMS on your device check below simple example. Here I have created a ListAdapter, by using this class sms will be showing on list and in this I have also created a SmsFormat class In which I have set value data. Below code will clearly described you how to make function to show sms.

    Step(1)-MainActivity-

    public class MainActivity extends ListActivity {
    
            @Override
            protected void onDestroy() {
                super.onDestroy();
            }
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                List<SmsFormat> List = new ArrayList<SmsFormat>();
    
                Uri uri = Uri.parse("content://sms/inbox");
                Cursor cur = getContentResolver().query(uri, null, null, null, null);
                startManagingCursor(cur);
    
                if (cur.moveToFirst()) {
                    for (int i = 0; i < cur.getCount(); i++) {
                        SmsFormat sms = new SmsFormat();
                        if (cur.getColumnIndexOrThrow("body") > -1) {
                            sms.setBody(cur
                                    .getString(cur.getColumnIndexOrThrow("body"))
                                    .toString());
                        }
                        if (cur.getColumnIndexOrThrow("address") > -1) {
                            sms.setNumber(cur.getString(
                                    cur.getColumnIndexOrThrow("address")).toString());
                        }
    
                        String name = null;
                        name = Search(cur.getString(
                                cur.getColumnIndexOrThrow("address")).toString());
    
                        if (name != " ") {
                            sms.setName(name);
                            List.add(sms);
                        }
                        cur.moveToNext();
                    }
                }
                cur.close();
    
                setListAdapter(new ListAdapter(this, List));
    
            }
    
            @Override
            protected void onListItemClick(ListView l, View v, int position, long id) {
                SmsFormat sms = (SmsFormat) getListAdapter().getItem(position);
                Log.i("MSG", sms.getBody());
    
            }
    
            public String Search(String number) {
                Uri uri = Uri.withAppendedPath(
                        ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                        Uri.encode(number));
                String name = " ";
    
                ContentResolver contentResolver = getContentResolver();
                Cursor contactLookup = contentResolver.query(uri, new String[] {
                                BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
                        null, null, null);
    
                try {
                    if (contactLookup != null && contactLookup.getCount() > 0) {
                        contactLookup.moveToNext();
                        name = contactLookup.getString(contactLookup
                                .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                    }
                } finally {
                    if (contactLookup != null) {
                        contactLookup.close();
                    }
                }
    
                return name;
            }
        }
    

    Step(2)-Create ListAdpter class-

    public class ListAdapter extends ArrayAdapter<SmsFormat> {
    
        private final Context context;
        private final List<SmsFormat> smslist;
    
        public ListAdapter(Context context, List<SmsFormat> smsList) {
            super(context, R.layout.activity_main, smsList);
            this.context = context;
            this.smslist = smsList;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View rowView = inflater.inflate(R.layout.activity_main, parent, false);
    
            TextView senderNumber = (TextView) rowView.findViewById(R.id.textView1);
    
            senderNumber.setText(smslist.get(position).getName() + "\n"
                    + smslist.get(position).getNumber() + "\n" + "\n"
                    + smslist.get(position).getBody());
    
            return rowView;
        }
    
    }
    

    Step(3)-Create SmsFormat class-

    public class SmsFormat {
        private String number;
        private String body;
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getBody() {
            return body;
        }
    
        public void setBody(String body) {
            this.body = body;
        }
    
    }
    

    Step(4)Activity_Main layout-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
    </RelativeLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: