<span id="Label3"></p><p align="center"><p align="center"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14pt;">Android Data sharing</span></p></p><p align="center"><p align="center"></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">In the process of Android application development, the use of bundle class objects to pass data is not applicable in all scenarios, the simple intent class object can not be put into the bundle class Object. of course, It is not possible to deny that using the intent class object to open another activity or service and using the bundle class object to carry the data is a good form, and is very versatile, but not omnipotent.</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">There are a lot of ways to share data in a program, and here's a look at what's more common and what's more consistent with object-oriented and efficient and Secure.</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">1, sharedperferences</span></p></p><p align="left"><p align="left"><span style="font-family: ' Microsoft Yahei '; font-size:16px;" first of all, the data between two files in same moudle (activity interface) enjoy. add following code overloaded method oncreate () main class mainactivity that shares data. < span></span></p></p><pre><pre> <span style="color: #008080;">1 </span> sharedpreferences sharedata = getsharedpreferences ("shared_data" <span style="color: #000000;">,mode_world_writeable); </span> <span style="color: #008080;">2 </span> sharedpreferences.editor se =<span style="color: #000000;"> Sharedata.edit ( );</span> <span style="color: #008080;">3 </span> se.putstring ("shared_data1", "Hello world" <span style="color: #000000;" ); < span> <span style="color: #008080;">4 </span> <span style="color: #000000;">se.commit (); </span> <span style="color: #008080;">5 </span> Intent Intent = <span style="color: #0000ff; >new </span> Intent (mainactivity . <span style= " color: #0000ff;">this </span>, GetData. <span style="color: #0000ff;">class </span> <span style="color: #000000;" ); < span> <span style="color: #008080;">6 </span> startactivity (intent); </span></span></pre></pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">As you can see, the shared identifier ("shared_data") and pattern (mode_world_writeable) are specified when the shared object is acquired, which is very important as a unique identifier for building shared objects elsewhere as a read-write mode that determines the current shared Object. The following editor class object mainly completes the loading of the data to be shared to the shared object, in the form of key-value, where only a pair of data ("shared_data1"-"Hello world", the type of Value) is Loaded. When the commit () method is finally called, the shared data can be extracted in another Activity.</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">Here's what's done in the OnCreate () method that gets the data class Getdata.</span></p></p><pre><pre><span style="color: #008080;">1</span> sharedpreferences sharedata = getsharedpreferences ("shared_data"<span style="color: #000000;">, mode_world_readable);</span> <span style="color: #008080;">2</span> getData =<span style="color: #000000;"> (TextView) Findviewbyid (r.id.get_data);</span> <span style="color: #008080;">3</span> getdata.settext ("shared_data1 = +sharedata.getstring (" shared_data1 ","<span style="color: #0000ff;">default</span>"));</pre></pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">It is simple to call the GetString () method directly with a shared object to get value via Key.</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">The problem, however, is that the Sharedpreferences.editor class object is similar to the bundle when loading data, and also through Puttype (Key, typevalue), if type is intent or powerless.</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">2. Bundle</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">Bundle classes are used in conjunction with the intent class, and the following is a simple example. Sets the bundle class object when another avtivity is Started.</span></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">Bundle ();</span> <span style="color: #008080;">2</span> bundle.putstring ("Key"<span style="color: #000000;">, "Value");</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">New</span> Intent (mainactivity. <span style="color: #0000ff;"> </span>this, Otheractivity. <span style="color: #0000ff;">class</span> <span style="color: #000000;">);</span> <span style="color: #008080;">4</span> <span style="color: #000000;">Intent.putextras (bundle);</span> <span style="color: #008080;">5</span> startactivity (intent);</pre></pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">Get data in the Otheractivity class:</span></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">. getintent (). Getextras ();</span> <span style="color: #008080;">2</span> String value = bundle.getstring ("Key");</pre></pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">3. Encapsulation</span></p></p><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">Separating the claims for shared data completely from the use of files, minimizing the coupling by declaring all the data that needs to be shared in a dedicated class. It was intended to give all the data variables declared as static variables in the class, taking into account the same form that would be given next, and not as professional and secure as the latter, so Skipped. Inherits the application class to declare shared data and provides an interface for setting and extracting Data.</span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Sharedata<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Application {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">shareData1;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">getData1 () {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">shareData1;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>SetData1 (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">ShareData1) { </span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. ShareData1 =<span style="color: #000000;"><span style="color: #000000;">shareData1;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>}</pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">If there are multiple data, then the corresponding settings and extraction methods to achieve a number of, interested partners can use generic multiplexing to write more concise Code. Note that the methods in class Shareddata (the target is Data) are accessible throughout the Project's class, as Follows.</span></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">int</span> shareData1 = ((sharedata) getapplicationcontext ()). getData1 ();</pre></pre><p align="left"><p align="left"><span style="font-family: ‘Microsoft YaHei‘; font-size: 16px;">So the point is, if you declare an object of the intent class in a class, fortunately the usage is the same as the int type, then it solves the problem of sharing between the different Files. This is always done with the intent class object, because in some cases it is really necessary to use this Method. If the Startactivityforresult () method cannot be directly exploited in the subclass of the inheriting service class, there is no chance of overloading the Onactivityresult () method, but it is also used in the subclass Onactivityresult The () method returns the intent class Object. now, you can put the activity you want to open in a class that can overload the Onactivityresult () method (such as the main class), and assign the returned intent object to the intent class object in the Sharedata class, and then Ok.</span></p></p><p><p>Android Data sharing</p></p></span>
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