Several ways that Android transmits data via Putextra

Source: Internet
Author: User

When passing data through intent, use the following code to error:

Hmap<string, object> map= (map<string, object>) parent.getitematposition (position); Intent.putExtra (" Userinfomap ", map);

However, it is normal to use the following code:

Hashmap<string, object> map= (hashmap<string, object>) parent.getitematposition (position); Intent.putextra ("Userinfomap", map);

This is why, as a small white I searched the internet, found that only the data can be serialized to put in, intent transfer data methods have the following:

Intent PutExtra (string name, string[] value) Intent PutExtra (string name, parcelable value) Intent PutExtra (string name, lo ng value) Intent PutExtra (string name, Boolean value) Intent PutExtra (string name, double value) Intent PutExtra (string name , parcelable[] value) Intent PutExtra (string name, char value) Intent PutExtra (string name, int[] value) Intent PutExtra (Str ing name, int value) Intent PutExtra (string name, double[] value) Intent PutExtra (string name, short value) Intent PU TExtra (string name, long[] value) Intent PutExtra (string name, boolean[] value) Intent PutExtra (string name, short [] value) Intent PutExtra (string name, String value) Intent PutExtra (string name, Serializable value) Intent p  Utextra (string name, float[] value) Intent PutExtra (string name, Bundle value) Intent PutExtra (string name, byte[] Value) Intent PutExtra (string name, charsequence value) Intent PutExtra (string name, char[] value) Intent put Extra (String name, byte ValUE) Intent Putextras (Intent src) Intent putextras (Bundle extras) 
You can see several types of data that can be passed, including basic data types (arrays with basic data types), strings (containing arrays), parcelable (containing arrays), Serializable, bundles, charsequence, and intent. For data of the map type (superclass) and Long,integer,double,byte,float,short, the value cannot be passed in this way because the serialized interface is not implemented

For how serialized data is passed, you can pass it in the following way

Submit Activity

Lst.setonitemclicklistener (New Onitemclicklistener () {public        void Onitemclick (adapterview<?> parent, View view,      int position, long id) {          HashMap map= (HashMap) parent.getitematposition (position);     Intent intent=new Intent ();     Intent.setclass (btsallinfoactivity.this,btsmapactivity.class);     Intent.putextra ("Bts_map", map);            StartActivity (intent);         }   });

Receive data activity
Intent intent=getintent ();  Map= (HASHMAP) Intent.getserializableextra ("Bts_map");    System.out.println ("111111111111");  System.out.println (Map.get ("bt_id"));  System.out.println (Map.get ("Bt_name"));  System.out.println (Map.get ("Bt_lon"));  System.out.println (Map.get ("Bt_lat"));  System.out.println ("111111111111");
For the bundle type of data, you can use the following methods:

Submit Activity

HashMap map= (HASHMAP) parent.getitematposition (position);     Intent intent=new Intent ();     Intent.setclass (btsallinfoactivity.this,btsmapactivity.class);     Bundle bundle = new bundle ();        Bundle.putserializable ("Bts_map", map);     Intent.putextras (bundle);          StartActivity (Intent);



Receive activity
Bundle bundle = Getintent (). Getextras ();    Map= (HASHMAP) bundle.getserializable ("Bts_map");    System.out.println ("111111111111");  System.out.println (Map.get ("bt_id"));  System.out.println (Map.get ("Bt_name"));  System.out.println (Map.get ("Bt_lon"));  System.out.println (Map.get ("Bt_lat"));  System.out.println ("111111111111");

For data of type parcelable

For Android, the transfer of complex types, mainly the conversion of their own classes to the underlying byte array, the activity between the data passed through intent implementation.

There are two main ways to implement an serializable interface, or to implement an Parcelable interface, for Android serialization objects.

The implementation of the serializable interface is javase itself support, and parcelable is the unique features of Android, efficiency than the implementation of serializable interface high,

It can also be used in interprocess communication (IPC). Implementation of the Serializable interface is very simple, declare it.

The implementation of the Parcelable interface is slightly more complex, but more efficient, and it is recommended to improve performance in this way.
Parcelable interface: The implementation of an instance of the Parcelable interface can write its own state information (state information usually refers to the value of each member variable) to parcel,

You can also restore its status from parcel. Parcel is used to complete the serialization of data transfer.

For the delivery of parcel data, you can use the following methods to pass

The steps to serialize an object by implementing the Parcelable interface: 1. Implement the Parcelable interface. 2. and implement the public void Writetoparcel (Parcel dest, int flags) method of the Parcelable interface. 3. A custom type must contain a static member named Creator, which requires the implementation of the Parcelable.creator interface and its methods.
Entity classes implement interfaces:

public class Person implements parcelable {public static final parcelable.creator<person> Creator = new Creator        <Person> () {@Override public person[] NewArray (int size) {return null;            } @Override Public Person Createfromparcel (Parcel source) {Person result = new person ();            Result.age = Source.readint ();            Result.name = Source.readstring ();            result.address = Source.readparcelable (Address.class.getClassLoader ());        return result;    }    };    private address address;    private String name;     private int age;    @Override public int describecontents () {return 0;    } public Address getaddress () {return address;    } public void Setaddress (address address) {this.address = address;        } @Override public void Writetoparcel (Parcel dest, int. flags) {Dest.writeint (age);        Dest.writestring (name); Dest.writeparcelaBLE (address, parcelable_write_return_value);    } public String GetName () {return name;    } public void SetName (String name) {this.name = name;    } public int Getage () {return age;    public void Setage (int.) {this.age = age; } @Override Public String toString () {return ' person [address= + address + ', name= "+ name +", age= "+ A    GE + "]"; } }

The official documentation says that using parce between different processes uses bind,public void Sendbroadcast (View viwe) {    log.d ("Test", Thread.CurrentThread () within the same process . GetName () + "sent a message to broadcast");    person person = new person ();    Person.setage ();    Address address = new address ();    Address.setstreet ("Yan Ling nan Lu");    Person.setname ("Carlos");    Person.setaddress (address);    Intent Intent = new Intent ("AAAA");    Intent.setclass (this, sendactivity.class);//    Intent.putparcelablearraylistextra (name, value)    Intent.putextra ("person", person);    StartActivity (intent);    }




Reference: http://xiaomi4980.blog.163.com/blog/static/21594519620141236363445/

Http://www.cnblogs.com/carlosk/archive/2012/09/28/2707521.html

Several ways that Android transmits data via Putextra

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.