我想通過短信發送CallLog
歷史記錄。那麼CallLog
歷史顯示在TextView
,但SMS不起作用。如何通過短信在Android中發送呼叫記錄歷史記錄?
代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_log);
tv = (TextView) findViewById(R.id.call);
getCallDetails();
}
private void getCallDetails() {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, null,null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch(dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
String str = sb.toString();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("5554", null, str, null, null);
}
你需要在後臺只發送短信或者是好的,如果短信撰寫屏幕會進入圖片? –
@PratikSharma在背景 – DroidLearner
好吧。查看我在下面發佈的解決方案。我已經驗證了這個解決方案,它對我來說工作得很好。 –