2013-10-24 71 views

回答

0

是的,你可以這樣做,默認Android系統使用SmsManager類來發送短信。您需要實現ContentObserver類以獲取有關出局短信的詳細信息。

//代碼訪問

private SmsSentObserver smsSentObserver = null; 
private static final Uri STATUS_URI = Uri.parse("content://sms"); 

if(smsSentObserver == null) 
{ 
    smsSentObserver = new SmsSentObserver(new Handler(), context); 
    context.getContentResolver().registerContentObserver(STATUS_URI, true, smsSentObserver); 
} 

// ContentObserve類

@SuppressLint("SimpleDateFormat") 
public class SmsSentObserver extends ContentObserver 
{ 
    private SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss"); 
    private static final Uri STATUS_URI = Uri.parse("content://sms"); 
    private Context context; 

    public SmsSentObserver(Handler handler, Context context) 
    { 
     super(handler); 
     this.context = context; 
    } 

    public boolean deliverSelfNotifications() {return true;} 

    public void onChange(boolean selfChange) 
    { 
     try 
     { 
      Cursor sms_sent_cursor = context.getContentResolver().query(STATUS_URI, null, null, null, null); 
      if (sms_sent_cursor != null) 
      { 
       if (sms_sent_cursor.moveToFirst()) 
       { 
        if (sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol")) == null ) 
        { 
         if (sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type")) == 2); 
         { 
          StringBuffer msgDetail = new StringBuffer(); 
          msgDetail.append("\n Sent To : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address"))); 
          msgDetail.append("\n" + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body"))); 
          msgDetail.append("\n Date : " + sdf.format(sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date")))); 
          LogFile logFile = new LogFile(context); 
          logFile.appendData(msgDetail.toString().trim() , context.getString(R.string.LogFileName)); 
          logFile = null; 
          msgDetail = null; 
         } 
        } 
       } 
      } 
     } 
     catch(Exception e) 
     { 
      LogFile logFile = new LogFile(context); 
      logFile.appendData(context.getString(R.string.SENT_SMS_ERROR) + e.toString() , context.getString(R.string.LogFileName)); 
      logFile = null; 
     } 
     finally { System.gc(); } 
     super.onChange(selfChange); 
    } 
} 
相關問題