我需要建议...我的应用程序与联系人合作,他们有通信(短信对话)。
我在优化获取过程中遇到了一些困难。我决定实现这个方案:
- 首次启动时,读取所有“content://sms/inbox”并将唯一值(按联系人数量分组)添加到数组 new String [n.count()][4]。(字段:姓名、号码、日期、消息和类型)
- 根据需要对数组进行排序并传输到主要活动(或者我可以直接在那里读取数据库表没关系)
- 主要活动类有程序,从接收数据中填充 ListView (list.setAdapter) 。
一切都在模拟器上完美运行,但是当我尝试在真实设备应用程序上进行设置时,会遇到致命错误。
所以在上述之后我有两个问题:1.为什么应用程序崩溃了?(任何想法) 2. 我如何修复/重建我的算法。任何中肯的批评。我需要在 ListView 联系人中显示的功能,如果联系人没有任何传入短信,则显示“-”,如果存在短信,则显示短信。
谢谢。
下面你可以看到从 db 插入和读取数据的过程。我会重复一遍,一切都在模拟器上完美运行。这意味着创建数据库、创建表以及插入、读取和更新数据。我公开代码“以获得完整的图片”。
public void Contact_to_db () {
dbHelper = new DBHelper(this);
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, "DATE ASC");
int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );
int indexDate = cursor.getColumnIndex( SmsReceiver.DATE );
if ( indexBody < 0 || !cursor.moveToFirst() ) return;
String sender = "";
String number = "";
String message = "";
String type = "";
do {
sender = "";
message = "";
type = "";
number = cursor.getString( indexAddr );
if (getContactDisplayNameByNumber(cursor.getString( indexAddr )).equals(""))
sender = cursor.getString( indexAddr );
else
sender = getContactDisplayNameByNumber(cursor.getString( indexAddr ));
message = cursor.getString( indexBody );
SQLiteDatabase db_read = dbHelper.getReadableDatabase();
Cursor c = db_read.query(dbHelper.USER_TABLE, null, "number like " + number , null, null, null, null);
if (c.moveToFirst())
//fiend...
else {
Pattern pattern = Pattern.compile("[0-9]");
Matcher matcher = pattern.matcher(sender);
boolean look = matcher.lookingAt();
if (look)
type = "1";
else
type = "0";
SQLiteDatabase db_whrite = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", sender);
cv.put("number", number);
cv.put("msg", message);
cv.put("date", cursor.getString( indexDate ));
cv.put("type", type);
db_whrite.insert(dbHelper.USER_TABLE, null, cv);
db_whrite.close();
}
c.close();
db_read.close();
}
}
while( cursor.moveToNext() );
cursor.close();
dbHelper.close();
}
阅读并填写...
public void UpdateTableRows() {
SMSDataCollection = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = null;
String message = "";
SQLiteDatabase db_read = dbHelper.getReadableDatabase();
Cursor c = db_read.query(dbHelper.USER_TABLE, null, null, null, null, null, null);
prmax = c.getCount();
cnt = 0;
pbCount.setMax(prmax-1);
pbCount.setProgress(0);
int i = 0;
if (c.moveToFirst()) {
do {
message = c.getString(3);
if (!(message.equals(""))) {
if ((message.substring(0, 2).equals("SM")) && (message.length()>6)) {
try {
String CutMsg = message.substring(message.indexOf('|')+1, message.length());
String key = message.substring(2, message.indexOf('|'));
Pattern pattern = Pattern.compile("[0-9]");
Matcher matcher = pattern.matcher(key);
boolean look = matcher.lookingAt();
if (look)
message = StringCryptor.decrypt(CutMsg, Integer.parseInt(key));
} catch (Exception e) {
e.printStackTrace(); }
} else
message = StringCryptor.decrypt(message, uPassword);
}
if (message.length() > 25)
message = message.substring(0, 24) + "...";
map = new HashMap<String,String>();
map.put(KEY_SENDER, c.getString(1));
map.put(KEY_NUMBER, c.getString(2));
map.put(KEY_MESS, message);
if (c.getString(4).equals("0"))
map.put(KEY_ICON, "person_icon_a");
else
map.put(KEY_ICON, "person_icon_f");
SMSDataCollection.add(map);
h.post(updateProgress);
} while (c.moveToNext());
}
c.close();
db_read.close();
dbHelper.close();
final BinderData bindingData = new BinderData(this,SMSDataCollection);
list.post(new Runnable(){
public void run() {
list = (ListView) findViewById(R.id.SMSList);
list.setAdapter(bindingData);
list.setEnabled(true);
//pbCount.setVisibility(View.GONE);
pbRound.setVisibility(View.GONE);
img_update.setVisibility(View.VISIBLE);
}
});
}