0

Why do we use Application context in sms Manager while we are Using Intent.What is the role which played application context in sms manager.please tell me... String no=mobileno.getText().toString();
String msg=message.getText().toString();

            //Getting intent and PendingIntent instance  
            Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
            PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  

            //Get the SmsManager instance and call the sendTextMessage method to send message  
            SmsManager sms=SmsManager.getDefault();  
            sms.sendTextMessage(no, null, msg, pi,null);  

            Toast.makeText(getApplicationContext(), "Message Sent successfully!",  
                Toast.LENGTH_LONG).show();  

1 Answers1

0

Why do we use Application context in sms Manager while we are Using Intent

You do not have to. In this case, this should be just fine for the places you are using getApplicationContext().

This blog post by Dave Smith explains the roles of different contexts.

What is the role which played application context in sms manager.

The Application Context has nothing specific to do with SmsManager, or Toast for that matter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Then again why do we use mainactivity.class we can write mainactivity.this instead of application context and mainactivity.class. – Saikiran Raju Nov 12 '14 at 13:09
  • @SaikiranRaju: `MainActivity.this` is an instance of an `Activity` (presumably, given the class name). `MainActivity.class` is an instance of a `Class` object. The `Intent` constructor that you are using takes a `Class` object as the second parameter, identifying the component (activity, service, etc.) for which the `Intent` is intended. – CommonsWare Nov 12 '14 at 13:12
  • Instance means here the mainactivity it is an member variable of activity?? – Saikiran Raju Nov 12 '14 at 13:28
  • @SaikiranRaju: "Instance" here means "instance". http://stackoverflow.com/questions/5126082/what-exactly-is-an-instance-in-java – CommonsWare Nov 12 '14 at 13:31