我正在使用代碼從設備獲取所有消息。在一個活動中執行時它可以正常工作,但由於某種原因,它在服務中不起作用。從Android設備獲取所有消息
我使用下面的代碼:
public class GetMessageData {
Context context;
GetMessageData(Context context){
this.context = context;
}
public JSONObject GetSMSDetails()
{
JSONObject result = null;
JSONArray jarray = null;
String link[] = {"content://sms/inbox","content://sms/sent","content://sms/draft"};
try {
jarray = new JSONArray();
result = new JSONObject();
Uri uri = Uri.parse("content://sms/");
Cursor c= context.getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
result.put("body",c.getString(c.getColumnIndexOrThrow("body")).toString());
result.put("date",c.getString(c.getColumnIndexOrThrow("date")).toString());
result.put("read",c.getString(c.getColumnIndexOrThrow("read")).toString());
result.put("type",c.getString(c.getColumnIndexOrThrow("type")).toString());
if((c.getString(c.getColumnIndexOrThrow("type")).toString()).equals("3"))
{
//Cursor cur= getContentResolver().query("", null, null ,null,null);
//startManagingCursor(cur);
String threadid = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
Cursor cur= context.getContentResolver().query(Uri.parse("content://mms-sms/conversations?simple=true"), null, "_id ="+threadid ,null,null);
startManagingCursor(cur);
if(cur.moveToFirst())
{
String recipientId = cur.getString(cur.getColumnIndexOrThrow("recipient_ids")).toString();
cur= context.getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
startManagingCursor(cur);
if(cur.moveToFirst())
{
String address = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
result.put("address",address);
cur.close();
}
}
}else
{
result.put("address",c.getString(c.getColumnIndexOrThrow("address")).toString());
}
jarray.put(result);
result = new JSONObject();
c.moveToNext();
}
}
c.close();
result.put("smslist", jarray);
//result = new JSONObject(jarray.toString());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
在這段代碼中,我在startManagingCursor()
收到錯誤。 同時搜索時,我發現startManagingCursor()
是已棄用。
你會得到什麼錯誤? – germi
我收到錯誤信息:無法解析startManagingCursor()。我試過使用context.startManagingCursor(),但它不工作。 – Sanju