0

I am creating an app which will give a toast message, whenever the device receives an incoming sms, even when the app is running in the background it should show a toast message.

I am testing my app on a Marshmallow 6.0 device (Moto M). Using Android Studio for development api 23 marshmallow.

MainActivity.java:

package android.broadcastreceiversms;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

IncomingSMS.java class file in same directory using Broadcast Receiver:

package android.broadcastreceiversms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Mahesh on 09-01-2017.
 */

public class IncomingSms extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();
        SmsMessage[] messages = null;
        String strMessage = "";

        if (myBundle != null) {
            Object[] pdus = (Object[]) myBundle.get("pdus");

            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++) {
                    String format = myBundle.getString("format");
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);

                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }

            Log.v("SMS", strMessage);
            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.broadcastreceiversms">
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <receiver android:name=".IncomingSms">
                <intent-filter android:priority="500">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>


        </activity>
    </application>

</manifest>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mahesh Jaganiya
  • 155
  • 1
  • 2
  • 11
  • Is the bundle null by any chance? – Cory Roy Jan 10 '17 at 18:25
  • you mean the app interface ? gui? yes there is nothing ,a blank hello world app – Mahesh Jaganiya Jan 10 '17 at 18:27
  • Your answer is here. Please remove this question. This is a duplicate. http://stackoverflow.com/a/12333749/3145960 – Reaz Murshed Jan 10 '17 at 18:56
  • Creating a Toast from a BroadcastReceiver isn't necessarily a good thing to do. It's possible your BroadcastReceiver may be 'alive' when some other Activity is in use. In this case, the Toast will make no sense to the user of the device. But if you still want to show toast, move your `receiver` out of activity tag. – Stanojkovic Jan 10 '17 at 18:58
  • @Stanojkovic I know its not a good thing , but i am just testing that it is working , then i want the program to change the Ringing profile on the incoming message. – Mahesh Jaganiya Jan 11 '17 at 03:10
  • Have a look at [this post](http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). – Mike M. Jan 12 '17 at 12:52

0 Answers0