Intercept android SMS going to default inbox - BroadcastReceiver - java

I'm developing a SMS based chat application using some Telco APIS.
I am receiving SMS only from a particular number and I want to know how I can prevent SMS being sent to the the default inbox of android.
Following is the code of my SMS receiver.
Compile SDK: 27
public void onReceive(Context context, Intent intent) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
this.intent = new Intent(BROADCAST_ACTION);
this.context = context;
chatlogDBAdapter = new ChatlogDBAdapter(context);
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
if(senderNum.equals("77100")){
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
String timestamp = String.valueOf(System.currentTimeMillis());
String[] messagearray = message.split(",", -1);
String username = messagearray[0];
String messagebody = messagearray[1];
chatlogDBAdapter.addMessage(username,username,"username",messagebody,timestamp);
msgusername = username;
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);
}
}
abortBroadcast();
return;
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
And the manifest
<receiver
android:name=".IncomingSms"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_DELIVER_ACTION" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

As of KitKat only one app can consume received sms directly with SMS_DELIVER_ACTION: changelog blog here.
Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4.

how I can prevent SMS being sent to the the default inbox of android.
Its not possible unless you make your app default SMS app. Starting from Andriod Kitkat only SMS default app has capability to Write data to the provider.
You can send/receive Binary SMS which won't be visible in the Inbox/Sent box

Related

Broadcast Receiver Not receive messages from Mobile default messags application Why?

In BroadcastReceiver i want to receive sms from default Mobile application.
But when user send sms without internet connect then i receive, but when user (send sms via wifi or Mobile Network) i unable to read message.
public class MessageReciver extends BroadcastReceiver {
private static MessageListener mListener;
public static final String reciveSMS="android.provider.Telephony.SMS_RECEIVED";
public static final String TAG ="SmsBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("fsdfdsfdsfdgfdsg "," rerwerw");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");
String formate = data.getString("format");
for (int i = 0; i < pdus.length; i++) {
SmsMessage smsMessage = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], formate);
} else {
smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
String message = smsMessage.getMessageBody();
Toast.makeText(context, "Message Received: " + message, Toast.LENGTH_SHORT).show();
}
}
}
}
I have't understand why i face this problem , because Broadcast receiver can send or receive broadcast messages from the Android system and other Android apps. It only receive (sms without using internet), But every mobile messages application have default enable option(Use wifi or data for messaging when available).
Anyone please help receive sms from default Mobile Messages Application
already register your receiver yet?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(messageReciver , new IntentFilter(YOUR FILTER));
}
try this
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application>
// add this
<receiver android:name=".MessageReciver">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( ContextCompat.checkSelfPermission(
this,
Manifest.permission.RECEIVE_SMS
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.RECEIVE_SMS}, 1);
}
}
It's true, since some month before I have the same problem. If wifi is off and LTE is off, then MMS and SMS actions are broadcasted with sms_received or wap_push_received are recognized by broadcast receiver intent.
In new Smartphones these messages are marked by green color.
But if the sender phone turn on wifi or turn on LTE data mode, then shit happens.
These sms or mms over wifi or over LTE are now CHAT messages. These chat messages are not broadcasted by sms_received or wap_push_received. These CHAT messages are blue marked on modern phones. If you try to send messages (sms or mms) to an smartphone with wifi off or LTE off, you will be informed that now your messages will be send only as normal SMS or MMS.
You can find these CHAT messages over contentresolver content://im/chat
Unfortunately I doesn't know yet, how to receive broadcasted chat messages. For now I pull every 1 second the contentresolver for new chat messages.

How to start (Register) service on android boot (Start up)?

Am new to android, I would like to develop an app which can receive SMS and process the message.
I have taken a look around the web on how to receive an SMS and bellow is the code am attempting to use.
public class SmsListener extends BroadcastReceiver {
String R_Message;
private SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
}
return;
} catch (Exception e) {
//Log.d("Exception caught",e.getMessage());
R_Message = "No message received";
return;
}
}
}
}
}
and here is the code i found that it can start a service so that my app can be working on background
public class SMSService extends IntentService {
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public SMSService() {
super("SMSService ");
}
#Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
}
}
ref: https://developer.android.com/guide/components/services.html
I created file(s) SmsListener.java and SMSService.java in manifest i do have
<receiver android:name=".SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Permission:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
But i don't know how to call the above two java processes and where to call them so i can verify the app is receiving SMS and it ll continue even when closed.
Note: My app will have two services which needs to be working every-time on background please show me how to add the second service.
Any help is greatly apprenticed thanks
As receivers are already declared in manifest file
SmsListener class will be automatically triggered when ever an message is received
you can process this info in smslistner
for more info refer https://developer.android.com/reference/android/content/BroadcastReceiver.html
I observe the service was suppose to be started within one of my activities as follow
Intent intent = new Intent(this, SMSservice.class);
startService(intent);
This has made it possible for the service to start.

Android: Pass value from activity to broadcastreceiver

First of all, sorry for my bad grammar. I am developing Auto Reply Message Application using broadcastreceiver i have problem with when I can receive value from activity to broadcastreceiver, the Auto Reply won't work. But if I'm not receive value from Activity, it's working. this is my activity code, I put it in onSensorChanged()
Intent i = new Intent("my.action.string");
i.putExtra("apaan", lala);
sendBroadcast(i);
and this is my broadcastreceiver class
public class AutoReply extends BroadcastReceiver{
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("message", "Broadcast Received: " +action);
if (action.equals(SMS_RECEIVED) && action.equals("my.action.string")) {
Bundle bundle = intent.getExtras();
String state = bundle.getString("apaan");
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] sms = new SmsMessage[pdus.length];
String isiSMS = "", noPengirim = "";
for (int i = 0; i < pdus.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
isiSMS = sms[i].getMessageBody();//mengambil isi pesan dari pengirim
noPengirim = sms[i].getOriginatingAddress();//mengambil no pengirim
}
String message = state;//isi balasan autoreplay
SmsManager smsSend = SmsManager.getDefault();
smsSend.sendTextMessage(noPengirim, null, message, null, null);
}
}
}
and this is my manifest
<receiver android:name=".AutoReply" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="my.action.string" />
</intent-filter>
</receiver>
The action will never be both android.provider.Telephony.SMS_RECEIVED and my.action.string.
Change this:
if (action.equals(SMS_RECEIVED) && action.equals("my.action.string")) {
To this:
if (action.equals(SMS_RECEIVED) || action.equals("my.action.string")) {
In addition, you're not sending pdus in the manually created Intent, so when you call bundle.get("pdus") it will return null. Then when you call pdus.length it will throw a NullPointerException.

Android not detecting SMS Sent using SMS Listener

I have following code
<receiver android:name=".SMSListener">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
public class SMSListener extends BroadcastReceiver
{
private SharedPreferences customSharedPreference;
#Override
public void onReceive(Context context, Intent intent)
{
customSharedPreference = context.getSharedPreferences("UserSharedPrefs", Activity.MODE_PRIVATE);
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
Editor editor = customSharedPreference.edit();
editor.putString("lastSmsReceived", String.valueOf(System.currentTimeMillis()));
editor.commit();
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
else if(intent.getAction().equals("android.provider.Telephony.SMS_SENT"))
{
Editor editor = customSharedPreference.edit();
editor.putString("lastSmsSent", String.valueOf(System.currentTimeMillis()));
editor.commit();
Log.d("SMS Sent", "SMS Sent");
}
}
}
With permissions
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
It detects SMS READ But SMS Sent is not detected. What else am I missing
There is no such action android.provider.Telephony.SMS_SENT. Android does not broadcast sent messages.
There is an alternative solution described here:
Android Broadcast Receiver for Sent SMS messages?

Receive SMS via My application in android

I am developing an android application that would let people send and receive SMS to a unique number via my application. I can send SMS but it is appearing in INBOX message box!
I want it to appear in my application
I googled and find this but I do not want it to appear in Toast message, I want it like in What's app in android and how to save all the SMS from this number?
this is the code:
package net.learn2develop.SMSMessaging;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
As per: Can we delete an SMS in Android before it reaches the inbox?
Incoming SMS message broadcasts
(android.provider.Telephony.SMS_RECEIVED) are delivered as an
"ordered broadcast" — meaning that you can tell the system which
components should receive the broadcast first.
If you define an android:priority attribute on your SMS-listening
, you will then receive the notification before the
native SMS application.
At this point, you can cancel the broadcast, preventing it from
being propagated to other apps.
In your manifest, you should have something that looks like this:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Note android:priority="999".
In your onReceive() method, the first line should be abortBroadcast();. Timing does matter and you will get bundle from the intent.

Categories