Cannot read SMS in reading mode Android 5.0? - android

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

Related

Any way to send text message without getting a confirmation?

Is there any way to send a text message without getting a confirmation? My current code for this is:
String phoneNumber = "1234567";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + phoneNumber)); // This ensures only SMS apps respond
intent.putExtra("sms_body", "TESTING TEXT MESSAGE! IT WORKS!");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Yes you should use SmsManager. Here is an example:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

Android: How to send multiple sms via intent (diff phone no and body)

I'm using following code for sending text message via Intent ( can not ask for permission so smsmanager is not an option)
//Code from this question
// <http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent>
private void sendsms(String toContact, String text){
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact)));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact);
intent.putExtra("sms_body", text);
}
this.startActivity(intent);
}
and I'm calling this in a for loop :
for(int i = 0; i<4 ;i++) {
sendsms(phoneNo[i],smsBody[i]);
}
Now the problem is whenever user gets to this line, the void will be called 4 times, but user will only see the last message in the devices default messaging app ready to be sent, but to get to the other ones, user should press back on the device and if not, he/she would never see the other messages.
what I need to be done is using a method like startActivityForResult(); so each time user sends the message he would be redirected to my app, and then my app starts another activity for the next text message.
any idea?
Thanks in advance
First of all you need to create a String with your phones, for that you have to use "; " as separator, but take care with Samsung, in those devices you have to use ", ".
So your code has to be similar to this one:
private void sendSms(String phone1, String phone2){
String separator = "; "
if (Build.MANUFACTURER.toLowerCase().contains("samsung"))
separator = ", "
String phones = phone1 + separator + phone2...
Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phones, null));
intentSms.putExtra("sms_body","your text") // Just if you want to add text
startActivity(intentSms);
}

Android send SMS to multiple contacts using ArrayList

Im writing an app that sends an SMS to several contacts. The contacts numbers are stored in an ArrayList (was received from another activity). I am not able to use this ArrayList to pass several contacts to the built-in SMS android app. This is the code:
ArrayList<String> numbersArrayList=getIntent().getExtras().getStringArrayList("phoneNumbers");
String message= "this is a custom message";
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body", message);
smsIntent.putExtra("address", ??????????);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);
I can iterate and print these contacts to the LogCat the simple "for each" loop and overriding toString method.
Use this code..
String toNumbers = "";
for ( String s : numbersArrayList)
{
toNumbers = toNumbers + s + ";"
}
toNumbers = toNumbers.subString(0, toNumbers.length - 1);
String message= "this is a custom message";
Uri sendSmsTo = Uri.parse("smsto:" + toNumbers);
Intent intent = new Intent(
android.content.Intent.ACTION_SENDTO, sendSmsTo);
intent.putExtra("sms_body", message);
startActivity(intent);

Android send SMS from tablet using SMS intent?

I would like to know if it is possible to send a SMS from an Android tablet using the SMS intent? If this is not possible, what are my options?
I would like to know if it is possible to send a SMS from an Android tablet using the SMS intent?
There isn't really an "SMS" Intent. There are ACTION_SEND and ACTION_SENDTO Intent actions that could result in an SMS being sent.
With respect to "tablets", most devices with above-average screen sizes do not have telephony capability, and therefore cannot do anything with SMSes, let alone send them in response to startActivity() on some Intent.
what are my options?
If you absolutely have to be able to send SMS messages, add <uses-feature android:name="android.hardware.telephony"/> to your manifest, so your app will only be installed on devices that have telephony capability.
If you would like to send SMS messages if that is possible, but work around it if it is not possible, you will want to do three things:
Add <uses-feature android:name="android.hardware.telephony" android:required="false"/> to your manifest
Use PackageManager and hasSystemFeature() to see if you actually have telephony capability at runtime
For devices that have telephony capability, before you call startActivity() on your "SMS Intent", use PackageManager and queryIntentActivities() to see if there is anything on the device that will respond to that Intent, or wrap your startActivity() call in an exception handler to catch the
ActivityNotFoundException
String smsNumber = "your number here";
String smsText = "Your text";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
+ phoneNumber)));
Try this.
How to check if a tablet has sms service available:
Here the third solution which CommonsWare described in his answer as a method:
public static boolean hasSmsService(Context context)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:123456789"));
PackageManager pm = context.getPackageManager();
List<ResolveInfo> res = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(res.size() > 0)
{
return true;
}
return false;
}
}
This is wok for you. try it.........
Method :
CAll on button click event.....
sendSMS("Any text",number,sms_string);
Now, declare this one out of oncreate();
public static void sendSMS(String status, String phoneNumber, String message) {
Log.e("", "Page : " + status + ", No : " + phoneNumber
+ ",Message Length: " + message.length() + ", Message : "
+ message);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}

How can I execute default sms app?

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

Categories

Resources