我一直在尝试编写一个程序来获取手机上的所有消息,然后进行备份。但是当我运行程序时,它崩溃了。请我需要帮助。这是代码
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/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象