Read and Write sharedpreferences of other applications

Source: Internet
Author: User

General steps to read sharedpreferences from other programs:
Use creatpackagecontext () to create the context corresponding to other programs
Use the getsharedpreferences () method of the context to obtain the corresponding sharedpreferences object.
To write data, call the Edit () method of sharedpreferences to obtain the corresponding editor object.
Three Access Conditions
The sharer needs to set the access mode of sharedpreferences to global read or write.
The visitor needs to know the package name and sharedpreferences name of the sharer.
The visitor needs to know the name and type of each data to read the data correctly.

Read and Write sharedpreferences of other applications
Context mycontext = NULL; // generate global variables
Sharedpreferences perfer;
Sharedpreferences. Editor editor;

Mycontext = createpackagecontext ("cn.edu. zwu. tel", context. context_ignore_security); // note that exception handling is required. Visitors need to know the package name of the sharer.
Perfer = mycontext. getsharedpreferences ("lgs000", context. mode_world_readable + context. mode_world_writeable); // The first parameter is used to obtain the file, and the second parameter is writable to the file.
Editor = perfer. Edit (); // create an editable object

Perfer. getstring ("name", null) // read the displayed content

Editor. putstring ("name", ZH); // write content
Editor. Commit (); // save it every time you finish writing.

Code:

View code

1 package CN. ZJ. nb. Wl;
2
3 Import Android. App. activity;
4 Import Android. content. context;
5 import Android. content. sharedpreferences;
6 Import Android. content. PM. packagemanager. namenotfoundexception;
7 Import Android. OS. Bundle;
8 Import Android. util. log;
9 Import Android. View. view;
10 Import Android. View. View. onclicklistener;
11 import Android. widget. Button;
12 Import Android. widget. edittext;
13 Import Android. widget. Toast;
14
15 public class sharedpretest02activity extends activity {
16 context mycontext = NULL;
17 sharedpreferences perfer;
18 sharedpreferences. Editor editor;
19 edittext edtzh, edtmm, edtdh;
20 button btngg, btndq;
21 Toast toast;
22 // int mode = mode_world_readable + mode_world_writeable;
23 @ override
24 public void oncreate (bundle savedinstancestate ){
25 super. oncreate (savedinstancestate );
26 setcontentview (R. layout. Main );
27 try {
28 mycontext = createpackagecontext ("cn.edu. zwu. tel", context. context_ignore_security );
29} catch (namenotfoundexception E)
30 {
31 E. printstacktrace ();
32}
33
34 perfer = mycontext. getsharedpreferences ("lgs000", context. mode_world_readable + context. mode_world_writeable );
35 editor = perfer. Edit ();
36 edtzh = (edittext) findviewbyid (R. Id. edtzh );
37 edtmm = (edittext) findviewbyid (R. Id. edtmm );
38 edtdh = (edittext) findviewbyid (R. Id. edtdh );
39 btngg = (button) findviewbyid (R. Id. btngg );
40 btndq = (button) findviewbyid (R. Id. btndq );
41
42 btngg. setonclicklistener (mylistener );
43 btndq. setonclicklistener (mylistener );
44}
45 onclicklistener mylistener = new onclicklistener (){
46 @ override
47 Public void onclick (view v ){
48 string ZH = edtzh. gettext (). tostring ();
49 string Mm = edtmm. gettext (). tostring ();
50 string DH = edtdh. gettext (). tostring ();
51 switch (V. GETID ())
52 {
53 case R. Id. btndq:
54 if (perfer. Contains ("account") & perfer. Contains ("password") & perfer. Contains ("phone "))
55 {
56 edtzh. settext (perfer. getstring ("Account", null ));
57 edtmm. settext (perfer. getstring ("password", null ));
58 edtdh. settext (perfer. getstring ("phone", null ));
59}
60 else
61 {
62 toast = toast. maketext (sharedpretest02activity. This,
63 "the configuration information does not exist or is incomplete! ", 5000 );
64 toast. Show ();
65}
66 return;
67 case R. Id. btngg:
68 editor. putstring ("Account", ZH );
69 editor. putstring ("password", mm );
70 editor. putstring ("phone", DH );
71
72 log. V ("cat", ZH );
73 editor. Commit ();
74 toast = toast. maketext (sharedpretest02activity. This,
75 "your account:" + zh +
76 "Password:" + mm +
77 "Tel:" + DH, 5000 );
78 toast. Show ();
79 edtzh. settext ("");
80 edtmm. settext ("");
81 edtdh. settext ("");
82 return;
83}
84
85}
86 };
87}

:

A problem occurs: when data is written, the content of the original XML file is not changed. logcat output: applicationcontext couldn't rename fill data/data.../shared_prefs

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.