day4.27 Summary of _intent and application

Source: Internet
Author: User

First,Intent Object (1) What is Intent?

1) intention, Messenger

2) Value Object ( encapsulates data for data passing )

(2) Application of Intent object

1) Starter Kit (activity,service,broadcastreceiver)

2) Stop the service, release receiver Dynamic registration

3) data transfer ( between Components )

(3) principle and process of Intent object Realization

1) Package Intent Information ( What do you want to do )

2) encapsulating data Information ( for data transfer )

StartActivity (Intent)

StartService (Intent)

Sendbroadcastreceiver (Intent)

The intent is passed to the underlying system through the correlation of the context object , and the underlying component is found based on the specific Intent information encapsulated in the intent, and starts him.

(4) Intended object Application method

1) Explicit Intent ( explicitly specifying the component to start )

2) Implicit intent ( not explicitly specifying the component to start, just passing the string to the underlying system, the underlying to match the object )

Common configuration for an implicit intent object:( implemented in intent-filter )

1) Action (on behalf of specific intent )

2) Category (Representative category, environment)

3) data ( can specify Type )

Description: When you use the implicit intent to start activity , the activity 's intent-filter You need to add a default category in the

Implicit intent is generally used to launch other components across apps .

Extension:pendingintent ( deferred Intent ), This object can encapsulate an intent object inside.

Intent Object Other configuration:

1) setflags (int n): focus on some tags related to startup mode

2) Setdataandtype (Uri,type)

3) ...

Example 1: Explicit Intent vs. implicit intent

public class Mainactivity extends Activity {

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

}

an explicit intent

public void OnClick01 (View v) {

StartActivity (New Intent (This,otheractivity01.class));

Intent intent=new Intent ();

Intent.setclass (this, otheractivity01.class); Direct class name

Intent.setcomponent (New ComponentName (this, otheractivity01.class));

StartActivity (Intent);

Bundle options=// Package animation information

Activityoptions.makecustomanimation (This,

Android. r.anim.slide_in_left,// can customize it yourself

Android. r.anim.slide_out_right). Tobundle ();

StartActivity (intent,options);

Startactivities (intents)

}

public void onClick02 (View v) {

for this intent object to start activity, the underlying will automatically add a category

Intent intent=new Intent ("Action.other"); Adding strings

Intent.addcategory ("Category.other");

Intent.settype ("text/*");

StartActivity (Intent);

}

}

Example 2:Intent passing objects and starting the camera

Class Student implements Serializable{ Direct Implementation interface

Private static final long serialversionuid = -1811479799107069425l;

String name;

Public Student (String name) {

This.name=name;

}

@Override

Public String toString () {

return name;

}

}

Class Elephant implements Parcelable{ after implementing parcelable , override method

private String name;

private int age;

Public Elephant (String Name,int age) {

This.name=name;

This.age=age;

}

deserialization

Public Elephant (Parcel source) {

This.name=source.readstring ();

This.age=source.readint ();

}

@Override

Public String toString () {

return name+ "/" +age;

}

@Override

public int describecontents () {

TODO auto-generated Method Stub

return 0;

}

/** Serialization * /

@Override

public void Writetoparcel (Parcel dest, int flags) {

Dest.writestring (name);

Dest.writeint (age);

}

use this object to implement a deserialization operation

public static final Creator<elephant> creator=

New Creator<elephant> () {

deserialization

@Override

Public Elephant Createfromparcel (Parcel source) {

return new Elephant (source);

}

@Override

Public elephant[] NewArray (int size) {

TODO auto-generated Method Stub

return new Elephant[size];

}

};

}

public class Mainactivity extends Activity {

Private ImageView ImageView;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

imageview= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);

}

public void OnClick01 (View v) {

Intent intent=new Intent (this,otheractivity01.class);

Intent.putextra ("Flag01", true);

Bundle B=new Bundle ();//key/value

B.putboolean ("Flag02", false);

B.putstring ("flag03", "HelloWorld");

Intent.putextra ("Data", b);

can encapsulate serialized objects

Intent.putextra ("Student", New student (" Chen Jing "));

Intent.putextra ("Elephant", New Elephant (" Elephant ", +));

StartActivity (Intent);

}

Private Button btn;

public void onClick02 (View v) {

btn= (Button) v;

Startactivityforresult (

New Intent (This,otheractivity02.class),//100 = Request Code ( identifies which request this is )

}

activity initiated by the startactivityforresult method

This method is executed when it is closed

@Override

protected void Onactivityresult (int requestcode, int resultcode, Intent data) {

LOG.I ("TAG", "resultcode=" +resultcode);

if (requestcode==100&&resultcode==200) {

String Item=data.getstringextra ("Itemkey");

Btn.settext (item);

}else if (requestcode==101) {

Bundle Bundle=data.getextras ();

Bundle.keyset ();

Bitmap bitmap= (Bitmap) bundle.get ("Data");

Imageview.setimagebitmap (BITMAP);

}

}

@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}

@Override

public boolean onoptionsitemselected (MenuItem item) {

if (Item.getitemid () ==r.id.cap) {

Start the camera program

Startactivityforresult (

New Intent (Mediastore.action_image_capture),

101);

}

return super.onoptionsitemselected (item);

}

}

public class OtherActivity02 extends Listactivity {

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.LAYOUT.ACTIVITY_OTHER_ACTIVITY02);

Setlistadapter (New Arrayadapter<string> (

This, Android. R.layout.simple_list_item_1,

New string[]{"A", "B", "C"});

}

@Override

protected void Onlistitemclick (ListView l, View v, int position, long ID) {

String item= (String) l.getitematposition (position);

Intent intent=new Intent ();

Intent.putextra ("Itemkey", item);

Setresult (200,// Response code

Intent);// Set response data

Finish ();

}

}

Example 3: ImageView pop-up image enabled by default

public void OnClick01 (View v) {

Intent intent=new Intent(intent.action_view);

Intent.setdataandtype (

Uri.fromfile (

New File ("/mnt/sdcard/png_01.png")), "image/*");

StartActivity (Intent);

}

Second,application object (1) What is application?

1) a Context object in Android ( resource access Capability )

2) global Access Objects in Android ( life cycle Same as APP life cycle )

(2)application object application

1) provide global data access for application Components ( shared data )

2) record the corresponding data information ( not suitable for large amounts of data )

For example:

1) User information after successful login.

2) A small amount of data information to be used by each component.

(3) Application of Application object

1) write ( inherit application),

2) Register ( the name attribute of the application specified in the manifest configuration file )

3) life cycle (onCreate,......)

Description: I store data in application and can also store data directly with static attributes.

public class MyApplication extends application {

public static user user;

}

<application

android:name= "Com.tarena.elts.MyApplication"

day4.27 Summary of _intent and application

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: info-contact@alibabacloud.com 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.