Showing posts with label Context. Show all posts
Showing posts with label Context. Show all posts

How to Get the the Context.SENSOR_SERVICE

public static final String SENSOR_SERVICE was added in API level 1
Use with getSystemService(String) to retrieve a SensorManager for accessing sensors.

1. In the MainActivity.java file, add the below line to the import section.

 import android.content.Context;

2. In the MainActivity.java file, add the below line to the onCreate method. This will save the SENSOR_SERVICE to a string named svcName

String svcName = Context.SENSOR_SERVICE;

3. Compile and run!

Next Recommended Article: How to Declare a SensorManager

Resources:
Professional Android 4 Application Development by Reto Meier, pg 482
http://developer.android.com/reference/android/content/Context.html#SENSOR_SERVICE

How to Get a PackageManager Instance

public abstract PackageManager getPackageManager () was added in API level 1
Return PackageManager instance to find global package information.

1. Declare a PackageManager object named myPackageManager

2. In the onCreate method, add the below line. This will return a PackageManager instance and save it to the PackageManager object named myPackageManager

myPackageManager = getPackageManager();

3. Compile and run!

Resources:
Professional Android 4 Application Development by Reto Meier, pg 702
http://developer.android.com/reference/android/content/Context.html#getPackageManager()

How to Get the Context.LOCATION_SERVICE

public static final String LOCATION_SERVICE was added in API level 1
Use with getSystemService(String) to retrieve a LocationManager for controlling location updates.

1. In the MainActivity.java file, add the below line to the import section.

 import android.content.Context;

2. In the MainActivity.java file, add the below line to the onCreate method. This will save the LOCATION_SERVICE to a string named svcName

String svcName = Context.LOCATION_SERVICE;


How to Get the Global Application Context

public abstract Context getApplicationContext () was added in API level 1
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

1. Declare a Context object named myContext.

2. In the onCreate method, add the below line. This will return the context of the single, global Application object of the current process and save it to the context object named myContext.

myContext = getApplicationContext();

3. Compile and run!

How to Declare a Context object

Context was added in API Level 1
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

1. In the MainActivity.java file, add the below line to the imports section.

 import android.content.Context;

2. In the onCreate method, add the following line.

Context myContext;

3. Compile and run!

Resources:
http://developer.android.com/reference/android/content/Context.html

How to Get Context.NOTIFICATION_SERVICE

public static final String NOTIFICATION_SERVICE was added in API level 1
Use with getSystemService(String) to retrieve a NotificationManager for informing the user of background events.

1. In the MainActivity.java file, add the below line to the imports section.

 import android.content.Context;

2. In the onCreate method, add the line below. This will save NotificationService to a string named svcName

String svcName = Context.NOTIFICATION_SERVICE;

3. Compile and run! 

Next Recommended Article: How to Declare a NotificationManager

Resources:
http://developer.android.com/reference/android/content/Context.html#NOTIFICATION_SERVICE