How to Monitor Incoming SMS in Android API <19 & API >=19?

934 views Asked by At

Am try to Monitoring Incoming SMS in my App. From Android Side they done some change for Monitoring incoming SMS from API 19.

Can't Track incoming Message in Background(at the time of App not in Background & Mobile Screen in Off State) in API 23.

In Android Document said Can't Track Incoming SMS in Background if your app is not as a Default Message App From API 19.

Ref:- Getting Your SMS Apps Ready for KitKat

But its working on My API 21(LOLLIPOP) device. It will not working on API 23(Marshmallow) devices. When Devices Screen is on or the app is open means it Working in API 23(Marshmallow) also.

Can You Please Update Me Is there any Way to Track Incoming Message in Background in API 23(Marshmallow).

Manifest Code

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

<receiver
        android:name=".SMSReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="5822">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>

Here My Java Code

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver {

public static final String BROADCAST = "SMS_RECEIVER";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

Intent broadcast_intent;
String message_body = "", originAddress;

StringBuilder bodyText;

@Override
public void onReceive(Context context, Intent intent) {


    if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED)) {

        broadcast_intent = new Intent(BROADCAST);

        Bundle bundle = intent.getExtras();
        SmsMessage[] messages;

        if (bundle != null) {

            Object[] pdus = (Object[]) bundle.get("pdus");

            messages = new SmsMessage[pdus.length];

            bodyText = new StringBuilder();

            for (int i = 0; i < messages.length; i++) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    String format = bundle.getString("format");
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                } else {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                originAddress = messages[i].getOriginatingAddress().toUpperCase();
                bodyText.append(messages[i].getMessageBody());

            }

            message_body = bodyText.toString();

        }
    }
}
}
2

There are 2 answers

1
karanatwal.github.io On

Basically you can register a broadcast receiver to listen for SMS_Receive and check out the following.

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("mySMS");

if (bundle != null) {
    Object[] pdus = (Object[])bundle.get("pdus");
    SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);
    Log.i(TAG, "smsActivity : SMS is <" +  sms.getMessageBody() +">");


    String message = sms.getMessageBody();
    //required sms
    TextView tx = (TextView) findViewById(R.id.TextBox);
    tx.setText(message);            
} else
    Log.i(TAG, "smsActivity : NULL SMS bundle");
1
Fahim Al Mahmud Ashik On

At the beginning of introducing Android 6.0 (API 23) Google made a huge change in app permission technique.So for API >=23 users needs to grant permissions to apps while the app is running, not when they install the app

System permissions are divided into two categories, normal and dangerous:

  1. Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
  2. Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.

Permission for Accessing the phones sms manager is considered as Dangerous Permission and that's why it needs to be called programmatically inside your code where you are trying to use phones smsmanager.

You can request permission in your mainActivity like this

int hasSMSPermission = checkSelfPermission( Manifest.permission.SEND_SMS );
List<String> permissions = new ArrayList<String>();

if( hasSMSPermission != PackageManager.PERMISSION_GRANTED ) {
    permissions.add( Manifest.permission.SEND_SMS );
}

if( !permissions.isEmpty() ) {
    requestPermissions( permissions.toArray( new String[permissions.size()] ), REQUEST_CODE_SOME_FEATURES_PERMISSIONS );
}

And to receive the request result of permission and set up your brodcast receiver make an override method of onActivityResult like this:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch ( requestCode ) {
        case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
            for( int i = 0; i < permissions.length; i++ ) {
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
                    Log.d( "Permissions", "Permission Granted: " + permissions[i] );
                } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                }
            }
        }
        break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

see this link for more details : https://developer.android.com/training/permissions/requesting.html