4

I'm trying to send CallLog history via sms. Well the CallLog history is displaying in TextView but SMS is not working.

Code:

@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);
    }
DroidLearner
  • 2,115
  • 5
  • 31
  • 50

3 Answers3

3

Because it possible your message string length is more then one message length so use smsManager. divideMessage (String text) for sending message if string exceed SMS size limit as:

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

and make sure you have added following permission in AndroidManifest.xml :

 <uses-permission android:name="android.permission.SEND_SMS" />
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks! but Can't we able to send in a Single message? – DroidLearner Dec 15 '12 at 10:08
  • @Learner678 : just check if condition because i'm not confirm about message length that's why i have used 160 you can search for default message length and check if then it will work in both cases. try after changing `if(str.length()>130)` instead of `if(str.length()>160)` – ρяσѕρєя K Dec 15 '12 at 10:12
  • Alright! I have changed to some other digits, but still the message is breaking into more than one. – DroidLearner Dec 15 '12 at 10:19
  • @Learner678 : this is another issue dear just check it on real device or make an new question about this issue thanks do much – ρяσѕρєя K Dec 15 '12 at 10:50
  • I removed sms coding and displayed in `TextView`. Its working on Emulator. I tried running on real device, its showing error **12-15 16:54:36.660: E/AndroidRuntime(4278): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.CallLogProvider uri content://call_log/calls from pid=4278, uid=10094 requires android.permission.READ_CONTACTS** Already I included permission in Manifest. – DroidLearner Dec 15 '12 at 11:26
  • @Learner678 : ok post your Manifest – ρяσѕρєя K Dec 15 '12 at 11:27
  • 1
    Now I Included `android.permission.READ_CONTACTS` in Manifest and its working on real device. In emulator its working without adding `android.permission.READ_CONTACTS` – DroidLearner Dec 15 '12 at 11:31
1

Your Code is working well.

SmsManager sm=SmsManager.getDefault();

String phoneNumber="xxxxxxxxxx";
String message="test text message";

sm.sendTextMessage(phoneNumber, null, message, null, null);

I think you need to add this permission.

<uses-permission android:name="android.permission.SEND_SMS"/>

I tried it and it charged me too for 1 sent sms. :)

Hope it helps you.

THanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
0

to send call log in message

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

now the receiver side will deal it as one message i try this block of its send in multi part but receive in one block of message

sender side

sender side

receiver side

receiver side

Adiii
  • 54,482
  • 7
  • 145
  • 148