android - 从安卓手机获取所有短信

标签 android sms uri message

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

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

最佳答案

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

}
}

这是在 ListView 中显示短信,您可以自己添加线程。

关于android - 从安卓手机获取所有短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18353734/

相关文章:

android - 应用速度集成-Google Analytics(分析)

android - Eclipse 无法识别 Android 设备,但 adb 可以

android - 撤消Android应用程序上的谷歌帐户访问确认

iphone - 使用鞋/标志拦截 iOS 6 越狱 iPhone 中的短信

java - 是否可以获取 .jar 中的类包的目录?

android - 在 SharedPreferences 中存储和检索目录 Uri

android - 尽管build.gradle应用了com.android.application插件,但没有Gradle “build”任务

android - 如何在 SMS 应用程序中查看从我的应用程序内发送的消息?

javascript - 如何使用 Twilio 和 Node 设置 HTTP cookie?

android - 为什么 getBitmap 方法不起作用?