Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting all sms from an android phone

I have been trying to write a program that gets all the messages on a phone and then makes a backup copy. But when ever i run the program, it crashes. Please i need help. Here is the code

public class MainActivity extends ListActivity {

 private ProgressDialog m_ProgressDialog = null; 
    private ArrayList<SmsBox> m_orders = null;
    private OrderAdapter m_adapter;
    private Runnable viewOrders;
    Activity mcontext;
    int totalSMS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     m_orders = new ArrayList<SmsBox>();
        this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
        setListAdapter(this.m_adapter);

        viewOrders = new Runnable(){
            @Override
            public void run() {
                getSms();

            }
        };
        Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(MainActivity.this,    
              "Please wait...", "Retrieving data ...", true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        if(m_orders != null && m_orders.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<m_orders.size();i++)
            m_adapter.add(m_orders.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};
private void getSms(){
    try{

         m_orders = new ArrayList<SmsBox>();
        getSmss();
        Thread.sleep(5000);
        Log.i("ARRAY", ""+ m_orders.size());
      } catch (Exception e) { 
        Log.e("BACKGROUND_PROC", e.getMessage());
      }
      runOnUiThread(returnRes);
  }

class Sms {


    private String address = null;
    private String displayName = null;
    private String threadId = null;
    private String date = null;
    private String msg = null;
    private String type = null;
   private void Print() {

       SmsBox o1 = new SmsBox();
       o1.setAddress(address);
       o1.setDisplayName(displayName);
       o1.setThreadId(threadId);
       o1.setDate(date);
       o1.setMsg(msg);
       o1.setType(type);

       m_orders.add(o1); //}
   }
}

private ArrayList<Sms> getSmss() {
    ArrayList<Sms> apps = getAllSms(); /* false = no system packages */
    final int max = apps.size();
    for (int i=0; i<max; i++) {
        apps.get(i).Print();


    }
    return apps;
}

public ArrayList<Sms> getAllSms() {
   ArrayList<Sms> lstSms = new ArrayList<Sms>();
    Sms objSms = new Sms();
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = mcontext.getContentResolver();

    Cursor c = cr.query(message, null, null, null, null);
    mcontext.startManagingCursor(c);
    totalSMS = c.getCount();

    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            objSms = new Sms();
            objSms.displayName=c.getString(c.getColumnIndexOrThrow("_id"));
            objSms.address=c.getString(c
                    .getColumnIndexOrThrow("address"));
            objSms.msg=c.getString(c.getColumnIndexOrThrow("body"));
            objSms.threadId=c.getString(c.getColumnIndex("read"));
            objSms.date=c.getString(c.getColumnIndexOrThrow("date"));
            if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                objSms.type="inbox";
            } else {
                objSms.type="sent";
            }

            lstSms.add(objSms);

            c.moveToNext();
        }
    }
    // else {
    // throw new RuntimeException("You have no SMS");
    // }
    c.close();

    return lstSms;
}




public class OrderAdapter extends ArrayAdapter<SmsBox>{

     private ArrayList<SmsBox> items;

     public OrderAdapter(Context context, int textViewResourceId, ArrayList<SmsBox> items) {
             super(context, textViewResourceId, items);
             this.items = items;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
             View v = convertView;
             if (v == null) {
                 LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 v = vi.inflate(R.layout.row, null);
             }
             SmsBox o = items.get(position);
             if (o != null) {

                     TextView DN = (TextView) v.findViewById(R.id.displayName);
                     TextView AD = (TextView) v.findViewById(R.id.address);
                     TextView DT = (TextView) v.findViewById(R.id.date);        
                     TextView TI = (TextView) v.findViewById(R.id.threadId);
                     TextView TY = (TextView) v.findViewById(R.id.type);
                     TextView MG = (TextView) v.findViewById(R.id.msg);
                     if (DN != null) {
                           DN.setText("Name: "+o.getDisplayName());                            }
                     if(AD != null){
                           AD.setText("Version: "+ o.getAddress());
                     }
                     if(DT != null){
                           DT.setText("Version: "+ o.getDate());
                     }
                     if(TI != null){
                           TI.setText("Version: "+ o.getThreadId());
                     }
                     if(TY != null){
                           TY.setText("Version: "+ o.getType());
                     }
                     if(MG != null){
                           MG.setText("Version: "+ o.getMsg());
                     }
                    /* if(Image!=null){
                         Image.setImageDrawable(o.getIcon());}*/
             }
             return v;
     }

}
}

and the SmsBox class is:

public class SmsBox{

private String address = null;
private String displayName = null;
private String threadId = null;
private String date = null;
private String msg = null;
private String type = null;


public String getDisplayName() {
    return displayName;
}

public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getThreadId() {
    return threadId;
}

public void setThreadId(String threadId) {
    this.threadId = threadId;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}
}

Thanks a lot in advance

log file:

08-21 10:43:58.146: E/AndroidRuntime(27085): at com.example.test2sms.MainActivity$2.run(MainActivity.java:52) 08-21 10:43:58.146: E/AndroidRuntime(27085): at java.lang.Thread.run(Thread.java:856) 08-21 10:44:10.186: E/WindowManager(27085): Activity com.example.test2sms.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42c60130 that was originally added here 08-21 10:44:10.186: E/WindowManager(27085): android.view.WindowLeaked: Activity com.example.test2sms.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42c60130 that was originally added here 08-21 10:44:10.186: E/WindowManager(27085): at android.view.ViewRootImpl.(ViewRootImpl.java:409) 08-21 10:44:10.186: E/WindowManager(27085): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322) 08-21 10:44:10.186: E/WindowManager(27085): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234) 08-21 10:44:10.186: E/WindowManager(27085): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)

like image 632
uchman21 Avatar asked Aug 21 '13 09:08

uchman21


1 Answers

Try this code,

public List<String> getSMS(){
    List<String> sms = new ArrayList<String>();
    Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

    while (cur != null && cur.moveToNext()) {
        String address = cur.getString(cur.getColumnIndex("address"));
        String body = cur.getString(cur.getColumnIndexOrThrow("body"));
        sms.add("Number: " + address + " .Message: " + body);  
    }

    if (cur != null) {
        cur.close();
    }
    return sms;
}
like image 168
shiju B Avatar answered Sep 29 '22 07:09

shiju B