0

in my application i can set String to SharedPreferences with this code:

    SharedPreferences prefs = this.getSharedPreferences("com.example.app", getApplicationContext().MODE_PRIVATE);
    prefs.edit().putString("last__id", mLastID).commit ();

and i can get this seved data from SharedPreferences in service by :

SharedPreferences sharedPref = getSharedPreferences("com.example.app", getApplicationContext().MODE_PRIVATE);
        firstID = sharedPref.getString("last__id", null);

now i dont have problem in this time, but after start activity by service and change last__id, i can not get new commited value from service, you think whats my code problem, whats tip and tricks?

POST UPDATE:

after commenting H4SN on this topic i want to use Messenger:

i get this error now:

 ir.tsms fatal error : println needs a message
    java.lang.NullPointerException: println needs a message
            at android.util.Log.println_native(Native Method)
            at android.util.Log.e(Log.java:231)

pass data from Activity:

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle Recevied = msg.getData();
            String resp = Recevied.getString("Mkey");
        }
    };

    Messenger messenger = new Messenger(myHandler);
    Message msg = Message.obtain();

    Bundle data = new Bundle();
    data.putString("Mkey",mLastID);
    msg.setData(data);

get data from Service:

    Bundle data = new Bundle();
    String firstID = data.getString("Mkey");

2 Answers2

0

ContentProvider with a uri is an option. It might be overkill for just a small amount of data but this can make it so you can access data even from different applications. I'm sorry if this doesn't answer your questions but here is a great tutorial that helped me when I came across an issue like this.

http://www.vogella.com/tutorials/AndroidSQLite/article.html

Sloganho
  • 2,041
  • 3
  • 16
  • 20
0

you can send result from service back to activity by using messanger no need to write on external file(Shared preference). see the example code. https://stackoverflow.com/a/26632865/2058260

Community
  • 1
  • 1
H4SN
  • 1,482
  • 3
  • 24
  • 43
  • thanks sir. i want to send data from activity to service or save data in evert location and get data by service –  Nov 18 '14 at 09:17
  • you can use messengers for this. – H4SN Nov 18 '14 at 09:22
  • post line where you are getting null exception – H4SN Nov 18 '14 at 09:36
  • did you passed that messenger to the service ? – H4SN Nov 18 '14 at 09:38
  • i get null exception for this line : `String firstID = data.getString("Mkey");` post updated please review that. i'm using your sample code –  Nov 18 '14 at 09:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65128/discussion-between-andbee-and-h4sn). –  Nov 18 '14 at 09:51