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