DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android SMS Popup - Part Three: The Activity

Android SMS Popup - Part Three: The Activity

Tony Siciliani user avatar by
Tony Siciliani
·
Apr. 03, 12 · Interview
Like (0)
Save
Tweet
Share
10.24K Views

Join the DZone community and get the full member experience.

Join For Free

in part one , we captured sms messages using a broadcastreceiver. in , among a set of options, we chose to pass the needed sms information (sender, message and timestamp) as a serializable 'popmessage' object from the background to the foreground. in this section, we will construct the screen to show to our users.


what do we want?  three things basically:

  1. get the sms information from the background
  2. show that information to the user in a popup window
  3. allow the user to react to that message within the application, either by dismissing it, or by responding to it by using his/her phone's favorite sms program a button click away.


by now, you must have read the android developer guide section on  activities  . so let's start coding our  popsmsactivity  class which will provide our ui. for now, that class will be our only and main activity, declared in our android manifest:

<application ...>
     <activity  android:label="@string/app_name"
                android:name=".popsmsactivity" ...>
                  <intent-filter >
                     <action android:name="android.intent.action.main" />
                     <category android:name="android.intent.category.launcher" />
                  </intent-filter>
     </activity>
     ....
</application>


we declare a single activity, the  popsmsactivity  , as the main entry point to our little application. for our development purposes, we can launch our application anytime and see how the popup looks like, without having to wait for an sms to arrive. for the ui, there are several kinds of  layout  to choose from, and that is a lot of xml. what we need right now is something as simple as possible, so we will program dynamically an android alert dialog.


when we will start building more features, we will have to use a more sophisticated layout ui and replace the main launcher by a settings screen where we would do all kinds of customization, like choosing colors, sound effects,  having a popup only for specific 'vip' phone numbers etc... and even have a "preview" button to see how our popup window will look like before use, instead of launching the dialog directly as we will do now. but for now, here's the activity class:

 public class popsmsactivity extends activity {

    /** called when the activity is first created. */
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        // no need for xml layouts right now
        // we will use a dialog instead
        //setcontentview(r.layout.main); 

        // retrieve serializable sms message object
        // by the key "msg" used to pass it
        intent in = this.getintent();
        popmessage msg = (popmessage) in.getserializableextra("msg");

        // case where we launch the app to test the ui
        // i.e. no incoming sms
        if(msg == null){
                 msg = new popmessage();
		 con.setphone("0123456789");
		 msg.settimestamp( system.currenttimemillis() );
		 msg.setbody(" this is a test sms message!");
        }
        showdialog(msg);
    }


the showdialog() method creates a very basic ui, i.e. a typical android dialog with a text and two buttons:

      /***/
    private void showdialog(popmessage msg){

    	final string sender = msg.getphone();
    	final string body = msg.getbody();

    	final string display = sender + "\n"
                + msg.getshortdate( msg.gettimestamp() )+ "\n"
                + body + "\n";

        // display in alert dialog
    	alertdialog.builder builder = new alertdialog.builder(this);
    	builder.setmessage(display)
    	.setcancelable(false)
    	.setpositivebutton("reply", new dialoginterface.onclicklistener() {
    		public void onclick(dialoginterface dialog, int id) {
                      // reply by calling sms program
    		      smsreply(sender, body);
    		}
    	})
    	.setnegativebutton("close", new dialoginterface.onclicklistener() {
    		public void onclick(dialoginterface dialog, int id) {
                      // go back to the phone home screen
    	              gohome();
    		}
    	});
    	alertdialog alert = builder.create();
    	alert.show();
    }


this is how it looks like on the phone's screen:



what is left in order to have a very basic working application, is handling those two button clicks, and talk a bit about  implicit intents  . in part four.

source:  tony's blog  .

SMS Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Testing as a Service?
  • How Observability Is Redefining Developer Roles
  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • GPT-3 Playground: The AI That Can Write for You

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: