我正在尝试通过短信发送CallLog
历史记录。那么CallLog
历史记录显示在TextView
中,但短信不起作用。
代码:
@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);
}
答案 0 :(得分:3)
因为您的消息字符串长度可能超过一个消息长度,因此如果字符串超出SMS大小限制,请使用smsManager. divideMessage (String text)发送消息:
SmsManager sms = SmsManager.getDefault();
if(str.length()>160){
ArrayList<String> smses = smsManager.divideMessage(str);
smsManager.sendMultipartTextMessage("5554", null,
smses, null, null);
}
else{
sms.sendTextMessage("5554", null, smses, null, null);
}
并确保您在AndroidManifest.xml
中添加了以下权限:
<uses-permission android:name="android.permission.SEND_SMS" />
答案 1 :(得分:1)
您的准则运作良好。
SmsManager sm=SmsManager.getDefault();
String phoneNumber="xxxxxxxxxx";
String message="test text message";
sm.sendTextMessage(phoneNumber, null, message, null, null);
我认为您需要添加此权限。
<uses-permission android:name="android.permission.SEND_SMS"/>
我尝试了它,它也向我收取1发送短信的费用。 :)
希望它对你有所帮助。
感谢。
答案 2 :(得分:0)
发送消息中的呼叫记录
SmsManager sms = SmsManager.getDefault();
ArrayList<String> part = sms.divideMessage(sb.toString());
sms.sendMultipartTextMessage("number to whom u wanna send", null, part, null, null);
现在接收方将把它作为一条消息处理我在多部分尝试这个发送块然后在一个消息块中接收
发件人方
接收方