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

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.

Related

Broadcast Receiver for discover bluetooth devices not working

Hello i have followed the example in https://developer.android.com/guide/topics/connectivity/bluetooth and my broadcast receiver maybe is not working. I am running android 8.1 in my Android Xiaomi mi A1 device.
#Override
protected void onCreate(Bundle savedInstanceState) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Toast.makeText(MainActivity.this, deviceName, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, deviceHardwareAddress, Toast.LENGTH_SHORT).show();
mDeviceList.add(deviceHardwareAddress);
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,mDeviceList);
listV.setAdapter(arrayAdapter);
}
}
};
Thank you!
NOTE
For android Api 21 Broadcast receiver works fine is there a change i have to make to make it work for Android 8.1?
In manifest I have put the permissions and other services like start stop bluetooth and find paired devices are working fine...
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
You Need to Add Permission in code above Version M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1000);

Intercept android SMS going to default inbox - BroadcastReceiver

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

Receiving SMS messages in my app

I am creating an app that receives SMS text messages, I have created a class that extends BroadcastReceiver to receive and display SMS messagesin a toast within the app. I have also added permissions too my manifest along with the declaration of the receiver. The problem is it still does not seem to display the message. It will only appear on the top bar as a notification. Hopefully someone will be able to see where i am going wrong. Do i have to have some code in my mainactivity in order to display the message?? or should it display in my all my activities straight from the receiver clas?? code below:
UPDATE
I ended up deleting my receiver and recreating everything, now it seems to work fine
SmsReceiver.java:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras ==null)
return;
//toast
Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
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();
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);
context.sendBroadcast(in);
this.abortBroadcast();
}
}
}
The problem is it still does not seem to display the message. It will only appear on the top bar as a notification
do you know what is - Ordered Broadcast?
in case you don't know - it's a broadcast that receives synchronously (one after another) by all receivers that registered for this broadcast. you can set in the manifest receiver deceleration a priority , and each one of the receivers can control if the broadcast will pass on in the priorities chain to additional registered receivers by using the aboartBroadcast() method.
what's to this and receiving SMS? - the system sends ordered broadcast when SMS receives. if your receivers priority is lower then the "default SMS app" priority - then what usually happens is that the application that got the broadcast first will cancel the broadcast, and you'll never receives it.
to get the broadcast before the default SMS application you have to declare your receiver with high priority (providing priority = 1000 should do the trick).
without providing priority - it's automatically set to zero.
you can have a look on this example - How Can i INTERCEPT an Incoming SMS With A specific Text
also make sure you declared the permission to RECEIVE_SMS in the manifest
Try starting again and setting up your receiver like so....
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras ==null)
return;
//toast
Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
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();
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);
context.sendBroadcast(in);
this.abortBroadcast();
}
}
}
#Override
public void onReceive(Context context, Intent arg1) {
SmsMessage msgs[] = getMessagesFromIntent(arg1);
for (int i = 0; i < msgs.length; i++) {
SmsMessage mesg = msgs[i];
Toast toast = Toast.makeText(context, mesg.getDisplayMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
}
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
// retrieve the SMS message received ::
Object[] pdus = (Object[]) extras.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";
// Toast to display SMS ::
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
// Send SMS ::
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("put your phoneNumber(emulatorPort)", null, str, null, null);
}
}
}
--
Have nice time !
Don't forget to add permissions !

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.

BroadcastReceiver not triggered for SMS_RECEIVED on Galaxy S II

I have the latest update for my Samsung Galaxy S2 and now my application stop working (App don't receive sms). I have SMSReceiver extends from BroadcastReceiver with SMS_RECEIVED action.
It looks like this:
private class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
Log.i(TAG, "New broadcast receiver");
SharedPreferences settings = getSharedPreferences(TAG, 0);
if (settings.getString("authCode", "").equals("")) {
Log.i(TAG, "Get sms");
if (intent.getAction().toString().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
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]);
}
if (messages.length > -1) {
Log.i(TAG, "Read sms");
}
}
}
}
}
I register BroadcastReceiver like this:
mSmsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(100000);
registerReceiver(mSmsReceiver, filter);
Everything works on HTC Desire or Desire Z and android emulator. On Galaxy S2 my SMSReceiver don't receive any Intent when SMS came. I tried all solution but it didn't have any effect.
Probably a issue with the latest version of GoSMS
Had the same issue with the "android.net.conn.CONNECTIVITY_CHANGE" action on the Galaxy s2. Couldn't receive reconnection intent when the Broadcast receiver was registered in the code.
Registering the broadcast receiver in the manifest was the solution for me.

Categories