其他分享
首页 > 其他分享> > 如何通过Android中的短信发送呼叫记录历史记录?

如何通过Android中的短信发送呼叫记录历史记录?

作者:互联网

我正在尝试通过短信发送CallLog历史记录. CallLog历史记录显示在TextView中,但SMS无法正常工作.

码:

@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);
    }

解决方法:

因为您的消息字符串长度可能超过一个消息长度,因此如果字符串超出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" />

标签:android,sms,calllog
来源: https://codeday.me/bug/20190625/1286924.html