Department of Famous Doors Android (4)-Activities (activity), services (service), broadcast (broadcast), broadcast receiver (Broadcastreceiver)
Introduced
Use activity, service, broadcast, broadcastreceiver in Android
Activity-Used to perform functions
Service-equivalent to running in the background
Broadcast (broadcast)-for sending broadcasts
Broadcast Receiver (Broadcastreceiver)-for receiving broadcasts
Intent-for connecting the above components and passing messages between them
1, the basic use of demonstration activity, one activity to start another activity, to start another activity for its passing parameters, the activated activity returned parameters to the initiator of the activity
Main.java
Package com.webabcd.activity;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
public class Main extends activity {
TextView txt;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (R.layout.main);
txt = (TextView) This.findviewbyid (r.id.txt);
Txt.settext ("Activity 1");
Button btn = (button) This.findviewbyid (R.ID.BTN);
Btn.settext ("Start another activity");
Btn.setonclicklistener (New Button.onclicklistener () {
@Override
public void OnClick (View v) {
Instantiate the Intent, specifying the activity that needs to be started
Intent Intent = new Intent ();
Intent.setclass (Main.this, Myactivity.class);
Instantiate Bundle, set parameters to pass
Bundle Bundle = new Bundle ();
Bundle.putstring ("name", "WEBABCD");
Bundle.putdouble ("Salary", 100.13);
Assign the parameters that need to be passed to the Intent object
Intent.putextras (bundle);
StartActivity (Intent); Starts the specified Intent (does not wait for return results)
Main.this.finish ();
Starts the specified Intent and waits for the result to be returned
If the second argument is greater than or equal to zero, then the Onactivityresult () method is returned when the result returns
Startactivityforresult (Intent, 0);
}
});
LOG.D ("Mydebug", "onCreate");
}
callback function when a started activity returns a result
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = ACTIVITY.RESULT_OK) {
Bundle Bundle = Data.getextras ();
String name = bundle.getstring ("name");
Double salary = bundle.getdouble ("salary");
Txt.settext ("Activity 1" + "\ n Name:" + name + "\ n Salary:" + string.valueof (Salary));
}
}
@Override
protected void OnStart () {
TODO auto-generated Method Stub
Super.onstart ();
LOG.D ("Mydebug", "OnStart");
}
@Override
protected void OnStop () {
TODO auto-generated Method Stub
Super.onstop ();
LOG.D ("Mydebug", "onStop");
}
@Override
protected void Onrestart () {
TODO auto-generated Method Stub
Super.onrestart ();
LOG.D ("Mydebug", "Onrestart");
}
@Override
protected void OnPause () {
TODO auto-generated Method Stub
Super.onpause ();
LOG.D ("Mydebug", "OnPause");
}
@Override
protected void Onresume () {
TODO auto-generated Method Stub
Super.onresume ();
LOG.D ("Mydebug", "Onresume");
}
@Override
protected void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
LOG.D ("Mydebug", "OnDestroy");
}
}