Department of Famous Door Android (4)

Source: Internet
Author: User
Tags log stub

Department of Famous Doors Android (4)-Activities (activity), services (service), broadcast (broadcast), broadcast receiver (Broadcastreceiver)


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

Package com.webabcd.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. */
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (R.layout.main);

txt = (TextView) This.findviewbyid (;
Txt.settext ("Activity 1");

Button btn = (button) This.findviewbyid (R.ID.BTN);
Btn.settext ("Start another activity");
Btn.setonclicklistener (New Button.onclicklistener () {
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
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));

protected void OnStart () {
TODO auto-generated Method Stub
Super.onstart ();

LOG.D ("Mydebug", "OnStart");

protected void OnStop () {
TODO auto-generated Method Stub
Super.onstop ();

LOG.D ("Mydebug", "onStop");

protected void Onrestart () {
TODO auto-generated Method Stub
Super.onrestart ();

LOG.D ("Mydebug", "Onrestart");

protected void OnPause () {
TODO auto-generated Method Stub
Super.onpause ();

LOG.D ("Mydebug", "OnPause");

protected void Onresume () {
TODO auto-generated Method Stub
Super.onresume ();

LOG.D ("Mydebug", "Onresume");

protected void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();

LOG.D ("Mydebug", "OnDestroy");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.