6

我一直在尝试编写一个程序来获取手机上的所有消息,然后进行备份。但是,当我运行该程序时,它会崩溃。请我需要帮助。这是代码

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

}
}

SmsBox 类是:

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

非常感谢提前

日志文件:

08-21 10:43:58.146: E/AndroidRuntime(27085): 在 com.example.test2sms.MainActivity$2.run(MainActivity.java:52) 08-21 10:43:58.146: E/AndroidRuntime(27085):在 java.lang.Thread.run(Thread.java:856) 08-21 10:44:10.186: E/WindowManager(27085): Activity com.example.test2sms.MainActivity 已经泄露了窗口 com.android.internal.policy。 impl.PhoneWindow$DecorView@42c60130 最初添加在这里 08-21 10:44:10.186: E/WindowManager(27085): android.view.WindowLeaked: Activity com.example.test2sms.MainActivity 已经泄露了窗口 com.android.internal .policy.impl.PhoneWindow$DecorView@42c60130 最初添加在这里 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): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322) 08-21 10:44:10.186: E/WindowManager(27085): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234) 08-21 10:44:10.186: E/WindowManager(27085): 在android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)

4

3 回答 3

11

试试这个代码,

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;
}
于 2013-08-21T09:32:18.280 回答
5
   package com.example.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

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

@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);
        lv=(ListView) findViewById(R.id.list);
        lv.setAdapter(m_adapter);

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

//            }
//        };
//        Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
//        thread.start();

}
@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() {
//      Log.e("here","here");
//        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()+" ");
      }
    Log.e("here","here");
    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();
  }

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();
    }
    Log.e("apps",apps.size()+"");
    return apps;
}

public ArrayList<Sms> getAllSms() {
   Log.e("apps","here ");
   ArrayList<Sms> lstSms = new ArrayList<Sms>();
    Sms objSms = new Sms();
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = getApplicationContext().getContentResolver();

    Cursor c = cr.query(message, null, null, null, null);
    //mcontext.startManagingCursor(c);
    totalSMS = c.getCount();
    Log.e("apps else",totalSMS+" total");
    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();
    Log.e("apps",lstSms.size()+"");
    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;
     }

}
}

这是在列表视图中显示短信,您可以自己添加线程。

于 2013-08-21T10:44:09.550 回答
1

尝试在清单文件中添加 read_sms 权限

于 2013-08-21T09:31:42.250 回答