I am using the below code to call an MMS intent :
{
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mmsIntent.putExtra("address", temp);
mmsIntent.putExtra("sms_body", msgstr);
mmsIntent.putExtra(Intent.EXTRA_STREAM, mediaUri);
}
Here 'temp' is the string containing multiple numbers and differentiated with ';'. It's working fine when we use this code for only a single number but when i add multiple numbers it doesn't attached to the messaging app. I have tried the same thing with ',' to separate the phone numbers but it also doesn't work. Any help is appreciated.
Intent mmsIntent= new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:9858254511;9858526521"));
mmsIntent.putExtra("sms_body", msgstr);
mmsIntent.putExtra(Intent.EXTRA_STREAM, mediaUri);
startActivity(smsIntent);
Add a semicolon delimited list of phone numbers to "smsto:" as the URI in the Intent constructor. Also refer this LINK
protected void sendMsg(Context context, SmsMessage smsMessage) {
SmsManager smsMgr = SmsManager.getDefault();
ArrayList<string> smsMessageText = smsMgr.divideMessage(smsMessage.getMsgBody());
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
int AddresseesPerMessage = 10;
StringBuilder builder = new StringBuilder();
String delim = "";
for (ContactItem c:smsMessage.getAddresseeList()) {
// For every phone number in our list
builder.append(delim).append(c.getPhoneNumber().toString());
delim=";";
if (((smsMessage.getAddresseeList().indexOf(c)+1) % AddresseesPerMessage) == 0 || smsMessage.getAddresseeList().indexOf(c)+1 == smsMessage.getAddresseeList().size()) {
// using +1 because index 0 mod 9 == 0
for(String text : smsMessageText){
// Send 160 bytes of the total message until all parts are sent
smsMgr.sendTextMessage(builder.toString(), null, text, sentPI, deliveredPI);
}
builder.setLength(0);
delim="";
}
}
}
use this code...i hope its useful to you.
Related
I have an SMS incoming phone number 123456. I want to use an Intent to open the SMS message of the incoming phone number in the service. I tried two ways, but they give unsatisfied results
First way:
Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
The way only opens the SMS list. We have to look at the incoming phone in the list and requires one more step to open the content of incoming SMS phone. I want to ignore the step. It means it will directly go the content of the incoming SMS phone number.
Second way:
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", SMSphoneNumber);
smsIntent.putExtra("sms_body",SMSBody);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
This way can read the detail of incoming SMS phone number but it is in sending mode. I want to read SMS in reading mode.
How can I do it in Android L? Thank all
The question is related with How to open SMS intent to read (not send) message?. The different one is providing the source code for two cases: one is reading mode (first one) with requiring one more step, another one is sending mode.
Update: This is way what I get the SMS incoming phone number
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSender += smgs[i].getOriginatingAddress();
if (smgs[i].getMessageBody()!=null)
infoSMS += smgs[i].getMessageBody().toString();
else
infoSMS += ".";
}
Toast.makeText(context, "Phone in SMSReceiver is -" + infoSender + "Body is" + infoSMS, Toast.LENGTH_SHORT).show();
}
}
}
}
If you want to open the message in the default application then you will be needing the thread id of the message you received. What you can do is query the message db and get the thread id using your logic and then open it using the below intent -
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("content://mms-sms/conversations/" + sms.getThreadId()));
mContext.startActivity(intent);
This is my answer with support from #Gautam.
First, we have to get thread_id of an SMS by using the code
public static long getThreadId(Context context, String phoneNumber) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
Cursor cursor = contentResolver.query(uri, null, "thread_id IS NOT NULL) GROUP BY (thread_id AND address=?", new String[]{phoneNumber}, "date DESC");
long threadId=0;
while (cursor.moveToNext()) {
threadId = cursor.getLong(cursor.getColumnIndex("thread_id"));
}
cursor.close();
return threadId;
}
From thread_id, we can show SMS content in reading mode as follows:
long thread_id= getThreadId(getApplicationContext(), SMSphoneNumber);
Log.d(TAG,"==========THREAD ID"+String.valueOf(thread_id)+"=========");
smsIntent.setData(Uri.parse("content://mms-sms/conversations/" + thread_id));
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(smsIntent);
Hope it help someone
I have pending intent to show message status after send
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
This is my pending intent
deliveredPI = PendingIntent.getBroadcast(this, id, new Intent(DELIVERED), 0);
When sms is delivered, I want to get requestcode from PendingIntent to show which sms is delivered, but in onReceive() method I can't get requestcode
You should add your SMS id to the PendingIntent like this:
Intent deliveryIntent = new Intent(DELIVERED);
deliveryIntent.putExtra("id", id);
deliveredPI = PendingIntent.getBroadcast(this, id, deliveryIntent, 0);
Then, in onReceive() you can get the SMS id like this:
int id = intent.getIntExtra("id", -1);
if (id >= 0) {
// Got a valid id...
}
I add contact pictures to a widget dynamical. This is my code for this part:
for (int x = 0; x < this.appWidgetIds.length; x++){
int id = this.appWidgetIds[x];
RemoteViews rv = new RemoteViews(this.context.getPackageName(), R.layout.widget);
for (int i = 0; i < maxCount; i++){
String lookupKey = sortedItems.get(i).getLookupKey();
Tools.ToLog("LOOKUPKEY=" + lookupKey);
Bitmap bmp = Contact.getContactPicture(this.context, lookupKey);
if (bmp != null){
Intent intent = new Intent(context, ContactsWidget.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
intent.putExtra(ITENT_LOOKUPKEY, lookupKey);
Tools.ToLog("LOOKUPKEY - IDENT=" + intent.getStringExtra(ITENT_LOOKUPKEY));
RemoteViews itemView = new RemoteViews(this.context.getPackageName(), R.layout.widget_itemview);
itemView.setImageViewBitmap(R.id.widget_ImageView, bmp);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(this.context, 0, intent, 0);
itemView.setOnClickPendingIntent(R.id.widget_ImageView, actionPendingIntent);
rv.addView(R.id.widgetContainer, itemView);
}
}
appWidgetManager.updateAppWidget(id, rv);
}
I tested the Lookupkey and the lookupkey from the intent over the log and it works on this side (variable lookupKey == intent.getStringExtra(ITENT_LOOKUPKEY)). When I now receive the intent because I clicked on a contact picture the intent extra info is always the same. No matter which of the contact pictures I clicked. This is the receive code:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String lookupKey = intent.getStringExtra(ITENT_LOOKUPKEY);
Toast.makeText(context, "Lookup Key: " + lookupKey, Toast.LENGTH_SHORT).show();
//Contact.openContact(this.context, lookupKey);
}
super.onReceive(context, intent);
}
It's always the lookupKey from the first added contact. Do I have to clear the intent somehow before adding another contact in the first function or what is the problem?
You only have one PendingIntent.
Quoting the documentation:
If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid
Since you have the same operation (getActivity()) and the same Intent routing pieces each time, there is only one PendingIntent.
Rather than setting the action to be ACTION_WIDGET_RECEIVER, make it be unique for each that you are creating in your loops.
I am new to android . In my application I want to send sms with contacts. How can i send sms to other through my appplication
private String SENT = "SENT";
private String DELIVERED = "DELIVERED";
...
PendingIntent sentPI = PendingIntent.getBroadcast(this.context, this.getUniqueId(), new Intent(this.SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this.context, this.getUniqueId(), new Intent(this.DELIVERED), 0);
String smsNumber = "+123456";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsNumber, null, "sms content", this.sentPI, this.deliveredPI);
I registered my receiver to get SMS. When I receive SMS's, how can I execute the phone's default SMS app?
Can I use the intent send action to start the default SMS app?
It can be done in a couple of different ways. Here's one:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Here "number" is an array of strings with the numbers of contacts to whom you want to send sms to and "älldetails" is teh string you want to send.
String n = "";
for(int i = 0; i<sizesf ;i++)
{
if(i == (sizesf-1))
{
n = n + number[i];
}
else
n = n + number[i] + ";";
}
Log.d("numbers in intent", n);
Intent smsIntent = new Intent( Intent.ACTION_VIEW, Uri.parse( "smsto:"+ n) );
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", n );
smsIntent.putExtra("sms_body",alldetails);
startActivity(smsIntent);
}