public class MyActivity extends Activity
{

        public static MyActivity myactivity;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myactivity = this;

        // Check whether MyActivity is not launched by History (long press on
home)
        // if launched by History it may be get the un-wanted parameters had
been set by SmsReceiver before
        int flag = getIntent().getFlags();
        flag = flag & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
        if (flag != Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
        {
                //Get parameter had put by SmsReceiver
                Bundle bundle = getIntent().getExtras();
                if (bundle != null)
                        {
                        String smsbody = (String) bundle.getString("SMS");
                        if (smsbody != null)
                        {
                                //Do some thing with smsbody
                        }
                }
        }
}

public class SmsReceiver extends BroadcastReceiver
{

        MyActivity myactivity;

        @Override
        public void onReceive(Context context, Intent intent)
        {
                myactivity = MyActivity.myactivity;

                //---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";
                        if (i == msgs.length-1)
                        {
                                address = msgs[i].getOriginatingAddress();
                                Msg = msgs[i].getMessageBody().toString();
                        }
                }

                //---display the new SMS message---
                try
                {
                        // if MyActivity is running either Frontground or 
Background
                        context.startActivity(myactivity.getIntent()); // just 
for in case
MyActivity is Background

                        /* Do some thing with myactivity.xxxx or 
myactivity.yyyy()  */

                }
                catch (Exception e) //in case myactivity had been closed
                {
                        //Launch new myactivity with parameter SMS = str;
                        Intent i = new Intent(context, aContext.getClass());
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putExtra("SMS", str);
                        context.startActivity(i);
                }
        }
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to