0

I have two differents application in Android. I need send data from one to the other one. The first is a Activity where i put in this data value in sharedpreferences file:

SharedPreferences selector;
selector = getSharedPreferences("tecnologia", Context.MODE_WORLD_READABLE);
Editor editor = selector.edit();

select = (RadioButton) findViewById(opciones.getCheckedRadioButtonId());

switch (select.getId()) {

   case R.id.radio0:
      editor.putString("opcion", "US");
      editor.commit();
      break;
   case R.id.radio1:
      editor.putString("opcion", "UWB");
      editor.commit();
      break;
}

Intent i = new Intent(this, ServiceConexion.class);
startActivity(i);
finish();

In the second Activity wich i launch i check this file and is correct. Then i call a remote servie.

In this second service (second project) i read this file:

try {
        Context con = createPackageContext(
                "org.gradiant.sistole.locdisplay",
                0);
        SharedPreferences configuracion;
        configuracion = con.getSharedPreferences("tecnologia",
                MODE_PRIVATE);
        select = configuracion.getString("opcion", "00");
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But not always has the correct value. Why?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user3243651
  • 241
  • 2
  • 4
  • 14
  • 1
    why not use the intent to pass the data that you want to pass to the second activity? You can use putExtra() and getExtra() with intents to pass the data between the activities. – upenpat Mar 25 '14 at 09:07
  • yeah i try this in method onBind, but application call this method after that i need this value onCreat, i supose that is because not is a normal service, is a remote service – user3243651 Mar 25 '14 at 09:16
  • If you found the solution, add it as an answer. Don't edit it into your question. – The Guy with The Hat Mar 25 '14 at 21:34

2 Answers2

0

you can't access other application data. instead you can do., create a file in sd card or any location and then access it in both application.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • ummm.. i use this method other times and works, you can read sharedpreferences file of other applications, but i not know why this time not works fine, maybe problems is that not save correctly because the fist app continues open – user3243651 Mar 25 '14 at 08:50
  • check this . this might help you.http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – Waqar Ahmed Mar 25 '14 at 08:52
0

MODE_PRIVATE says it's private to that application and can't be accessed by other application. How ever you could achieve it in two ways:

  1. Create a file in sd card as the other answer says. But it's dangerous as any other app can read it.

  2. Use startActivityForResult(intent,requestCode) in the app that requests data and setResult in the other app.

P.S.: As far as I understood, both of them are your apps.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
  • http://developer.android.com/reference/android/content/ContentProvider.html could be useful for sharing data between applications – Droidman Mar 25 '14 at 08:53