Androidsharedpreference data sharing

Source: Internet
Author: User

One app accesses data from another app. The premise is that the data being accessed by the app must be globally visible.

Steps:

1. Get the context of another app

PackageName is the package name of the app

Other=createpackagecontext (PackageName, context_ignore_security);

2, Get Sharedpreferences object.


The specific demo code is as follows:

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Onphone (View v) {Intent Intent = new Intent (Intent.action_call,uri.parse ("Tel:" +getoherappxml (" Com.example.phonewrite ")); StartActivity (intent); MainActivity.this.finish (); System.exit (0);} private string Getoherappxml (String packagename) {Context other=null;try {other=createpackagecontext (PackageName, context_ignore_security); Sharedpreferences preferences=other.getsharedpreferences ("Config", other. mode_world_readable); return preferences.getstring ("Phone", "13047220888");} catch (Namenotfoundexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}

The key code for the application being accessed is as follows:

public class Mainactivity extends Actionbaractivity {private EditText medittext; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); mEditText= ( EditText) Findviewbyid (R.id.ed_write_phone);} public void Onphone (View v) {sharedpreferences preferences=getsharedpreferences ("Config", mode_world_readable); Preferences.edit (). putstring ("Phone", Medittext.gettext (). toString (). Trim ()). commit (); Toast.maketext (Mainactivity.this, "submitted successfully", 0). Show ();}}




Androidsharedpreference data sharing

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.