开发者

Java Android - Filter incoming text messages

I am after a way of:

  1. Getting my Android application to re开发者_如何学编程ceive an alert / prompt when a text message is received.
  2. Getting the latest message from the inbox. From there I want to scan/filter it, but am sure I can work that part out.

Anyone able to suggest where to start on either part? Which alert is triggered upon inbox changing size or new sms message received? And How can I retrieve messages from the inbox? Which permissions will need to be granted?


The answer to my question is mostly found here: http://www.kaloer.com/incoming-sms-messages

Hope this helps anyone else looking for the same answer in the future.


My solution to this question is:

public class MainActivity extends ListActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       List<SmsData> smsList = new ArrayList<SmsData>();

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

       // Read the sms data and store it in the list
       if(c.moveToFirst()) {
           for(int i=0; i < c.getCount(); i++) {
               SmsData sms = new SmsData();
               sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
               sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
              // sms.setName(c.getString(c.getColumnIndexOrThrow("name")).toString());
               smsList.add(sms);

               c.moveToNext();
           }
       }
       c.close();

       // Set smsList in the ListAdapter
       setListAdapter(new ListAdapter(this, smsList));

   }

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
       SmsData sms = (SmsData)getListAdapter().getItem(position);

       Toast.makeText(getApplicationContext(), sms.getBody(), Toast.LENGTH_LONG).show();

   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜