I used intent and pending intent to send message. The code is working fine when I sent some texts. The problem is that, while sending call logs as String by sms manger it never send it.
I can send simple text messages using this code but when i adding the string which copies call log it do not send the message.
I have given call log permission and send and receive sms permissions in manifest.
My code is here :-
MainActivity.java
package com.example.sendmessage;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendme(View v)
{
Cursor mCursor=managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
int number=mCursor.getColumnIndex(CallLog.Calls.NUMBER);
int date=mCursor.getColumnIndex(CallLog.Calls.DATE);
int duration=mCursor.getColumnIndex(CallLog.Calls.DURATION);
int type=mCursor.getColumnIndex(CallLog.Calls.TYPE);
StringBuilder sb=new StringBuilder();
while (mCursor.moveToNext())
{
String phnumber=mCursor.getString(number);
String callduration=mCursor.getString(duration);
String calltype=mCursor.getString(type);
String calldate=mCursor.getString(date);
Date mDate = null;
//---------------------------------------------------
String givenDateString = calldate;// dateTime variable is the variable in which you are storing the date currently
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
try {
mDate = (Date) sdf.parse(givenDateString);
// timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
//-------------------------------------------------
// Date d=new Date(calldate);
String callTypeStr="";
switch (Integer.parseInt(calltype)) {
case CallLog.Calls.OUTGOING_TYPE:
callTypeStr="OutGoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callTypeStr="Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callTypeStr="Missed";
break;
}
sb.append("Phone Number" + phnumber);
sb.append("Call duration" + callduration);
sb.append("call type" + callTypeStr);
sb.append("Date" + mDate);
sb.append("------------------------------");
sb.append(System.getProperty("line.separator"));
}
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
sms.sendTextMessage("9074560566", null, sb.toString().substring(0, 500), pi,null);
}
}
kindly please give a solution.