when a text message is sent form, lets say an application 'myApp' it'll open in default text message app of the receiver. but i want to control how it looks to receiver(like changing colour). Is there anyway to send text and read that text in native app, 'myApp'? Or identify it was sent from 'myApp' and import message to 'myApp'.
sure you can to receive messages make a broadcast receiver for ingoing messages an each time a message arrive start your activity that displays the message ...
public class SMSApp extends IntentReceiver {
private static final String LOG_TAG = "SMSApp";
/* package */ static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
public void onReceiveIntent(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
StringBuilder buf = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
buf.append("Received SMS from ");
buf.append(message.getDisplayOriginatingAddress());
buf.append(" - ");
buf.append(message.getDisplayMessageBody());
}
}
//start you messages activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.myMessagesAcivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//prepare message text to be sent to the activity via bundle
Bundle bundle = new Bundle();
bundle.putString("message", but.toString());
i.putExtras(bundle);
context.startActivity(i);
}
}
}
and in your manifest file add these permissions
<uses-permission android:id="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
and this receiver
<receiver class="SMSApp">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and to send SMS from your app
use this method
public void eb3atSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, **DummyClasshere.class**), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
Related
I'am working on SMS manager like app. Now when an SMS is received a new Dialog Activity like a pop-up which shows the user the Sender's number and message. It's OK but if another SMS comes before the user press Back button (the Pop-Up activity still there on the Top), then the new SMS can't be shown to the user.
Every new SMS should show up on the pop-up activity diminish any older SMS if on foreground.
I tried in and out the onReceive method of the Broadcast Receiver class. By digging deeper I found that if the pop-up with an SMS is on foreground, the Pop-up Activity cannot gets called. It just don't come to the OnCreate method.
BroadcastReceiver class:
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
public static String latestSMSnumber, latestSMScontent;
SmsMessage[] msgs = null;
String str = "";
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; ++i) {
// Convert Object array
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// Fetch the text message
str += msgs[i].getMessageBody().toString();
str += "\n";
latestSMSnumber = msgs[i].getOriginatingAddress();
latestSMScontent = str;
}
// Display the entire SMS Message
Log.e("TAG1 number: ", latestSMSnumber);
Log.e("TAG2 content: ", str);
latestSMScontent = str;
// Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
Intent i=new Intent(context.getApplicationContext(),NewSMS.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("Number", latestSMSnumber);
i.putExtra("MsgBody", latestSMScontent);
context.startActivity(i);
}
}
}
I like to show any new SMS with number and msg text in the pop-up window.
Write This in you manifest.xml file in activity tag NewSMS Activity
android:launchMode=”singleTop”
For more information Please read the bellow link
https://android.jlelse.eu/android-activity-launch-mode-e0df1aa72242
I've got an JavascriptInterface method that registers a LocalBroadcastManager listening to received SMS's. When an SMS arrives the onReceive method doesn't get called.
public void UIregisterSMSSource(String number) {
LocalBroadcastManager.getInstance(this)
.registerReceiver(mMessageReceiver, new IntentFilter(ACTION_SMS_RECEIVE));
}
/**
* SMS Receiver
*/
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SMS_RECEIVE)) {
StringBuilder buf = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Bundle extras = intent.getExtras();
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
Log.d(TAG, "[SMS] onReceive by " + sender + ". Content - " + body);
// Save preferences of the activation code
}
}
}
}
};
This is the ACTION_SMS_RECEIVE variable:
private static final String ACTION_SMS_RECEIVE = "android.provider.Telephony.SMS_RECEIVED";
I've tested this before as BroadcastaReceiver and it worked. I removed the receiver from the manifest too which I don't know if it's right.
Do I need to configure something more? On the examples I've came across there's no further configuration needed.
Thanks in advance.
Receiver mMessageReceiver can only listen the intent which sent via LocalBroadcastManager.sendBroadcast(intent).
Here, intent with android.provider.Telephony.SMS_RECEIVED action, is being raised by system (not using LocalBroadcastManager.sendBroadcast(intent)), so your app does not listen to this intent.
Your previous approach was correct, and you can continue on that logic.
You can read a detailed example of LocalBroadcastManager to make clear your doubts about its working flow.
I am having a requirement in my application where i need to integrate messaging in the app. I want to use android native SMS API to send the messages and receive them. The main challenge is that i don't want to show received messages in the Message application. All messages should be opened and send from my application only.
I have tried receiving following intent in my broadcastreceiver :
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
But when the message comes to my application at the same is is received by native Message application, which i don't want.
I have also tried sending data message on a specific port in emulator, it is sending the messgae but not received by my application as well as Message native app on other emulator.
Intent Filter is :
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<data android:port="8901"/>
<data android:scheme="sms"/>
</intent-filter>
I am using the sendDataMessage() function with the given port.
Is it possible send messages in a better and secure way so that there is no possibility to steal your data using the native SMS API in android ? If not what alternate I can go for implementing the same.
Here is what I had implemented and its working like exactly what i wanted.
After entering the phone number and Text message call this method .
private static final int MAX_SMS_MESSAGE_LENGTH = 160;
private static final int SMS_PORT = 8901;
private static final String SMS_DELIVERED = "SMS_DELIVERED";
private static final String SMS_SENT = "SMS_SENT";
private void sendSms(String phonenumber,String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
byte[] data = new byte[message.length()];
for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
{
data[index] = (byte)message.charAt(index);
}
manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
}
private BroadcastReceiver sendreceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String info = "Send information: ";
switch(getResultCode())
{
case Activity.RESULT_OK: info += "send successful"; break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: info += "send failed, generic failure"; break;
case SmsManager.RESULT_ERROR_NO_SERVICE: info += "send failed, no service"; break;
case SmsManager.RESULT_ERROR_NULL_PDU: info += "send failed, null pdu"; break;
case SmsManager.RESULT_ERROR_RADIO_OFF: info += "send failed, radio is off"; break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String info = "Delivery information: ";
switch(getResultCode())
{
case Activity.RESULT_OK: info += "delivered"; break;
case Activity.RESULT_CANCELED: info += "not delivered"; break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
Your Receiver for messages should look like :
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
public class MySMSReceiver extends BroadcastReceiver {
String action,from,message;
#Override
public void onReceive(Context context, Intent intent) {
action=intent.getAction();
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if(null != bundle)
{
String info = "Binary SMS from ";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
byte[] data = null;
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
info += msgs[i].getOriginatingAddress();
info += "\n*****BINARY MESSAGE*****\n";
from= msgs[i].getOriginatingAddress();
data = msgs[i].getUserData();
for(int index=0; index<data.length; ++index) {
info += Character.toString((char)data[index]);
message += Character.toString((char)data[index]);
}
}
}
Intent showMessage=new Intent(context, AlertMessage.class);
showMessage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
showMessage.putExtra("from", from);
showMessage.putExtra("message", message);
context.startActivity(showMessage);
}
}
I have created a simple activity AlertMessage.java to show received message.
The way I registered my broadcast receiver in Manifest :
<receiver android:name=".MySMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
The port mentioned here must be the same which we specified in method sendSMS() for sending the message.
UPDATE :
Github Repository of working project
https://github.com/pyus-13/MySMSSender
According to me it's possible. You can set higher priority to your app, like
<intent-filter android:priority="100">
in your manifest.xml. So that all messages will pass through your app at first and you can store those messages in your database. At the same time, if you are aborting your broadcast, say
abortbroadcast();
you will not get any notifications also on anywhere.
It's not possible to prevent default Messaging application from receiving SMS messages in any way, because your application can't alter other apps' permissions.
I am writing an app which will send an SMS automatically to a given number on some conditions, SMS code is working and SMS is also delivered.. but the sent SMS are not showing up in any of the other SMS Clients/Apps installed...
String phoneNumber = "1234512345";
String message = "Test Message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
What am i missing?
I want the sent SMS (sent by my app) to show up in other SMS apps which are installed..
Yes you have sent SMS progrmatically but you haven't informed SMS Content Provider so it will not get your message unless you inform it.
ContentValues values = new ContentValues();
values.put("address", "1234512345");
values.put("body", "Test Message");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Make sure to include below permissions in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
use broadcast receiver in receiving side application
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
String str="";
String from="";
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += messages[i].getMessageBody().toString();
from = messages[i].getOriginatingAddress().toString();
}
if(from.equals("123456789")){
str= str.trim();
abortBroadcast();
Intent intt = new Intent(context,MainActivity.class);
intt.putExtras("message",str);
intt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intt);
}
}
in your mainActivity write some code to display the obtained message. the message will be in the intent. so get the String from it.
here the broadcast receiver, receives messages from only number "123456789". the messages from this number will not go to inbox of ur mobile. if u want to see in inbox also remove the method abortBroadcast().
I am trying to receive the sms from a particular number in my android application and trying to show the message content in my application. But i am not able receive the sms.
the class which i am using to receive is given below.
public class SmsReceiver extends BroadcastReceiver {
StringBuilder sb;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[]) extras.get("pdus");
if (pdus.length < 1)
return; // Invalid SMS. Not sure that it's possible.
sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage
.createFromPdu((byte[]) pdus[i]);
if (sender == null)
sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null)
sb.append(text);
}
if (sender != null && sender.equals("********")) {
// Process our sms...
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS");
broadcastIntent.putExtra("data", sb.toString());
context.sendOrderedBroadcast(broadcastIntent, null);
System.out.println("MESSAGE FROM SERVER -->"
+ sb.toString());
this.abortBroadcast();
}
return;
}
}
}
}
After receiving the sms, i will check the sender and if it is the sender i am looking for then i will send another broadcast with the sms. There i will show the content.
see my manifest
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="SMS" />
</intent-filter>
</receiver>
I am not able to find a solution please help.
Things to check (from my experience):
android:priority="2147483647" is not valid, the largest value is 1000. Numbers larger than this are ignored
Insert your this.abortBroadcast(); in the for loop (to be called pdus.length times)
If the sent SMS matches your criteria, for testing, save it in your app preference and in your main activity read the value from your preference, if it worked, then the problem is with your broadcastIntent.