Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Saturday, June 2, 2012

To send SMS in android


To send SMS from an android app we need to include the SEND_SMS permission in the android manifest file.

<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
   
The code snippet to send an SMS is

private void sendSMS(String phonenumber, String message)
{       
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phonenumber, null, message, null, null);       
}

Here, SmsManager.getDefault() is the static method available with SmsManager class and we can call this method to send sms from or app. To use this function we need to import SmsManager class using “import android.telephony.gsm.SmsManager;”

Navigating from one page to another in android


In almost all apps we need to move from 1 page to another. In android we use classes to represent it and often referred as activity. In our project if we have more than 1 class we need to include all the classes in the manifest file using
<activity android:name="Classname"></activity>       
Here, the main class ClassA has got 2 buttons and on clicking the button1 we need to advance to Class1 and on clicking the button2 we need to advance to Class2, and the code snippet is as follows

public class ClassA extends Activity implements OnClickListener 
{
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
      
       final Button button2 = (Button)this.findViewById(R.id.button2);
       button2.setOnClickListener(new OnClickListener()
        {
        @Override
              public void onClick(View v)
              {
                     navigate();
              }   
        });

    }

       protected void navigate()
        {
              // TODO Auto-generated method stub
              Intent i = new Intent(this,Class2.class);
              startActivity(i);
         }

       @Override
       public void onClick(View v)
         {
              // TODO Auto-generated method stub
              Intent i = new Intent(this,Class1.class);
              startActivity(i);
         }
}

To start a new activity we use Intents. Intents help to combine loosely coupled modules. To use Intent in our code we need to import android.content.Intent class.