Accessing Resources :
During your application development you will need to access defined resources either in your code, or in your layout XML files. Following section explains how to access your resources in both the scenarios:
Accessing Resources in Code :
When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
Example:
To access res/drawable/myimage.png and set an ImageView you will use following code:
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Here first line of the code make use of R.id.myimageview to get ImageView defined with id myimageview in a Layout file. Second line of code makes use of R.drawable.myimage to get an image with name myimage available in drawable sub-directory under /res.
Example:
Consider next example where res/values/strings.xml has following definition:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>
Now you can set the text on a TextView object with ID msg using a resource ID as follows:
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
Example:
Consider a layout res/layout/activity_main.xml with the following definition:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
This application code will load this layout for an Activity, in the onCreate() method as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
Accessing Resources in XML
Consider the following resource XML res/values/strings.xml file that includes a color resource and a string resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>
Now you can use these resources in the following layout file to set the text color and text string as follows:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />
Now if you will go through previous chapter once again where I have
explained Hello World! example, and I'm sure you will have better
understanding on all the concepts explained in this chapter. So I highly
recommend to check previous chapter for working example and check how I have
used various resources at very basic level.
Android Activities
If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity lifecycle diagram:
Callback
|
Description
|
onCreate()
|
This is the first callback and called when the activity is
first created.
|
onStart()
|
This callback is called when the activity becomes visible
to the user.
|
onResume()
|
This is called when the user starts interacting with the
application.
|
onPause()
|
The paused activity does not receive user input and cannot
execute any code and called when the current activity is being paused and the
previous activity is being resumed.
|
onStop()
|
This callback is called when the activity is no longer
visible.
|
onDestroy()
|
This callback is called before the activity is destroyed
by the system.
|
onRestart()
|
This callback is called when the activity restarts after
stopping it.
|
Android Services:
A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. A service can essentially take two states:
State
|
Description
|
Started
|
A service is started when an application component,
such as an activity, starts it by calling startService(). Once
started, a service can run in the background indefinitely, even if the
component that started it is destroyed.
|
Bound
|
A service is bound when an application component
binds to it by calling bindService(). A bound service offers a
client-server interface that allows components to interact with the service,
send requests, get results, and even do so across processes with interprocess
communication (IPC).
|
A service has lifecycle callback methods that you
can implement to monitor changes in the service's state and you can perform
work at the appropriate stage. The following diagram on the left shows the
lifecycle when the service is created with startService() and the diagram on
the right shows the lifecycle when the service is created with bindService():
To create an service, you create a Java class that
extends the Service base class or one of its existing subclasses. The Service
base class defines various callback methods and the most important are given
below. You don't need to implement all the callbacks methods. However, it's
important that you understand each one and implement those that ensure your app
behaves the way users expect.
Callback
|
Description
|
onStartCommand()
|
The system calls this method when another component, such
as an activity, requests that the service be started, by calling startService().
If you implement this method, it is your responsibility to stop the service
when its work is done, by calling stopSelf() or stopService()
methods.
|
onBind()
|
The system calls this method when another component wants
to bind with the service by calling bindService(). If you implement
this method, you must provide an interface that clients use to communicate
with the service, by returning an IBinder object. You must always
implement this method, but if you don't want to allow binding, then you
should return null.
|
onUnbind()
|
The system calls this method when all clients have
disconnected from a particular interface published by the service.
|
onRebind()
|
The system calls this method when new clients have
connected to the service, after it had previously been notified that all had
disconnected in its onUnbind(Intent).
|
onCreate()
|
The system calls this method when the service is first
created using onStartCommand() or onBind(). This call is
required to perform one-time setup.
|
onDestroy()
|
The system calls this method when the service is no longer
used and is being destroyed. Your service should implement this to clean up
any resources such as threads, registered listeners, receivers, etc.
|
No comments:
Post a Comment