4

我正在尝试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);
    }
4

3 回答 3

3

因为您的消息字符串长度可能超过一个消息长度,所以使用smsManager。如果字符串超出 SMS 大小限制,则用于发送消息的divideMessage(字符串文本) :

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" />
于 2012-12-15T09:55:56.247 回答
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 条发送短信的费用。:)

希望它可以帮助你。

谢谢。

于 2012-12-15T09:59:54.017 回答
0

发送呼叫登录消息

SmsManager sms = SmsManager.getDefault();
ArrayList<String> part = sms.divideMessage(sb.toString());
sms.sendMultipartTextMessage("number to whom u wanna send", null, part, null, null);

现在接收方将其作为一条消息处理

发送方

发送方

接收端

接收端

于 2015-06-23T02:36:41.763 回答