1

I'm developing one sms application. In that once the SMS comes to inbox it should display some alert. I want to add alert in the following code. Is it possible?

public class SMSReceiver extends BroadcastReceiver
{
        @Override
        public void onReceive(Context context, Intent intent)
        {
                Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if(null != bundle)
        {

            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]);                


            }


        }
        }

}
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
user2012
  • 337
  • 1
  • 5
  • 13

4 Answers4

3

Create a Activity in your application and start Activity from your BroadcastReceiver

and now launch AlertDialog within onCreate method of Activity

create style.xml inside res/values folder and put this inside it

<style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">#000000</item>
        <item name="android:windowNoTitle">true</item>

    </style>

make Activty Transparent by putting android:theme="@style/Theme.Transparent" as a attribute of Activity in your AndroidManifest.xml file so only AlertDialog will be shown.

Ravi1187342
  • 1,247
  • 7
  • 14
  • this alert will it display even the application is not running in the foreground? – user2012 Apr 10 '12 at 11:08
  • yes it will be displayed. whenever you start your activity from from your `BroadcastReceiver` . if you write the code of `AlertDialog` within `onCreate()` method of `Activity` started from `BroadcastReceiver` – Ravi1187342 Apr 10 '12 at 11:12
  • ok. Is it possible to check the receiving part of application using emulator(application has port number) ? – user2012 Apr 10 '12 at 11:36
  • if you are using `BroadcastReceiver` to receive SMS, you can send SMS to emulator using DDMS to check the receiving part of application . – Ravi1187342 Apr 10 '12 at 11:49
  • Yes, i'm using BroadcastReceiver to receive the messages. But the messages should come to the application inbox(i.e., to specific port) not to the native inbox. Is it possible to check this in emulator? – user2012 Apr 10 '12 at 12:19
  • I think There is no way to send sms on specific port using DDMS. send sms normally using DDMS. and see what happens your BroadcastReceiver receives that SMS or not. – Ravi1187342 Apr 10 '12 at 12:51
  • @Ravi1187342 how to display the alert dialog after calling the broadcast receiver intent? – AndroidOptimist Dec 18 '13 at 06:58
  • @ AndroidOptimist start activity with transparent background and display Alert dialog inside activity's oncreat() or on resume() method, in this case only alert dialog will be displayed. – Ravi1187342 Dec 18 '13 at 07:02
1

As far as i have understood, u are not able to launch activity from a broadcast receiver. If that is the case, this will help you:

  Intent i=new Intent(context,ActivityName.class);
    startActivity(i);

and "how to show AlertDialog", following Ravi's answer is the best thing you can do.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

Call new activity set dialog theme and called from onReceive method of Broadcast Receiver..

SBJ
  • 4,089
  • 3
  • 24
  • 27
0

You should use a Notification Manager: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Specifically Google say (their emphasis):

A background service should never launch an activity on its own in order to receive user interaction. The service should instead create a status bar notification that will launch the activity when selected by the user.

You're talking about a BroadcastReceiver rather than a service, but I suspect it still applies.

pjcard
  • 1,088
  • 10
  • 22