Activity (active window):
The most basic module in the Android program, a visual user interface that is displayed for user action. An Android application can have only one activity, or it can contain multiple activity, and the amount and each activity's role depends on the application and its design.
(1) Life cycle of activity
Create → Run onCreate onStart onresume
Run → Destroy OnPause onStop ondestory
Run → stop (not visible, not operable) OnPause OnStop
Stop → Restore Onrestart onStart onresume
Run → pause (visible not operable) onPause
Pause → Restore onresume
Note: When activity is paused or stopped, the app may be destroyed if a higher priority application requires memory.
(2) Examples of applications: Data persistence
Save: OnPause onStop ondestory
Fetch: OnCreate onStart Onresume
-
Service: No interface, running in the background
(1) Service creation:
A. Creating a class inheriting Service
B. Overriding the Onbind () method
C. Registering in a manifest file android:name= "package name. Class Name"
(2) Start service
Intent serviceintent = new Intent (this,myservice.class);
StartService (serviceintent);
Note: The first boot will trigger the service's OnCreate and Onstartcommand methods, and if the service already exists, click Start Services multiple times, and only the Onstartcommand method will be called.
(3) Stop service
StopService (serviceintent);
(4) Binding service
Bindservice (serviceintent,xxx,xxx);
A. Serviceintent: Represents a intent object;
B. Second parameter: Represents a Serviceconnection object, for example:
Serviceconnection Conn =new Serviceconnection;
C. Third parameter: Context.bind_auto_create indicates that a service is automatically created if the service does not exist.
(5) Unbind service
Unbindservice (conn);
(5) Calling methods in service by binding
A. Create a class Mybinder inherit binder
B. Add a method in the service that you want to call in the class
C. In the Onbinder () method, returns an object of type Mybinder
D. In the onserviceconnected () method of the activity, get the IBinder type Object
E. Strongly convert IBinder type object to Mybinder type
Broadcastreceiver (broadcast receiver)
(1) Broadcast: single, Mass, as the Android internal information transmission mechanism.
(2) Creation of broadcast receivers:
A. Create a class myreceiver inherit broadcastreceiver;
B. Rewrite the OnReceive () method;
C. Registering in the manifest file
Android:name= "package name. Class Name"
(3) Apply yourself to send the broadcast
Create a Intent Object
Intent Intent = new Intent ();
Set Filter Criteria
Intent.setaction ("xxx");
Send broadcast
Sendbroadcast (Intent);
(4) Receiving broadcasts
A. Registering a broadcast in a manifest file
<intent-filter>
<action android:name= "xxx"/>
</intent-filter>
As long as the conditions match, you can receive the broadcast
B. Registering a broadcast in code
Create a Intentfilter object
Intentfilter filter = new Intentfilter ();
Add Filter conditions
Filter.addaction ("xxx");
Create a Myreceiver object
Myreceiver receiver = new Myreceiver ();
Register a broadcast
Registerreceiver (Filter,receiver);
Cancel a broadcast
Unregisterreceiver (receiver);
ContentProvider (data sharing)
A component used to provide data sharing, primarily to provide specific application data to other applications, which can be stored in a file system or SQLite database. In an Android program, the implementation of shared data needs to inherit from the ContentProvider base class, which implements a standard set of methods for using and storing data for other applications. However, the application does not call these methods directly, but instead uses a Contentresolver object and replaces it by calling its method. The Contentresolver object provides methods such as query, insert, and update to perform various operations on shared data.
(1) Contentresolver (content Resolver) Example: Reading system contacts
Create a Contentresolver object
Contentresolver resolver = Getcontentresolver ();
Use the Query method to get contact information
Cusor cusor = resolver.query (phones.content_uri, NULL, NULL, NULL, NULL);
Traverse Contact information
while (Cursor.movetonext ()) {
Gets the index position of the field based on the field name
int nameindex = Cursor.getcolumnindex (phones.name);
int numberindex = Cursor.getcolumnindex (Phones.number);
Get contact information by index
String name = cursor.getstring (Nameindex);
String number = cursor.getstring (Numberindex);
}
Close the Cursor object
Cursor.close ();