2013-08-21 38 views
4

我一直在試圖編寫一個程序,獲取手機上的所有消息,然後製作備份副本。但是當我運行該程序時,它會崩潰。請我需要幫助。下面是代碼從Android手機獲取所有短信

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

感謝很多提前

日誌文件:

10月8日至21日: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泄漏了窗口com.android.internal.policy。 [email protected]原來是在這裏添加的 08-21 10:44:10.186:E/WindowManager(27085):android.view.WindowLeaked:Activity com.example.test2sms.MainActivity泄露了窗口com.android。 [email protected]原來是在這裏添加的 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):at android.view .WindowManagerImpl.addView(WindowManagerImpl.java:234) 08-21 10:44:10。 186:E /窗口管理器(27085):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:153)

回答

5

試試這個代碼,

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

它的工作原理!將用它來嘗試和建設我的..謝謝 – uchman21

+0

然後接受答案 –

1

嘗試增加read_sms許可清單檔案中的

+1

請發佈logcat輸出 –

+0

我已添加它 – uchman21

+0

已添加read_sms權限但它不斷崩潰 – uchman21

4
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短信,你可以自己添加線程。

+0

謝謝你,讓先生阿迪亞萊 –