2

I want to access EditText value in service but I don't no exact way of doing that. I have done this code.

Context context = getApplicationContext; 
Edittext et = (EditText)context.findViewById;

But it doesn't give me findViewById method.

Can anyone give me some hint that how can I access EditText value in service.

n1nsa1d00
  • 856
  • 9
  • 23
  • check my ans http://stackoverflow.com/questions/30817775/does-anyone-know-how-to-get-edittext-value-in-service/30817796#30817796 – Tufan Jun 13 '15 at 10:49
  • can you explain why you need to access shared ET using services ? – N Jay Jun 13 '15 at 10:49
  • Nader I am trying to achieve some new things in android. It's easy to access EditText in Activity that is in foreground but i want to check that how we can access in service. –  Jun 13 '15 at 11:02

3 Answers3

3

Following Code may help you to get Edittext in Service

context = getApplicationContext();
Activity a=(Activity)context;
EditText editText = (EditText)a.findViewById(R.id.editText1);
Sanjay Bhalani
  • 329
  • 1
  • 18
1

first u need to bind the service,

in activity,you can get the service reference in the service connection. and pass EditText reference to service. and then set the text to it(check reff of edittext is null or not).

balu b
  • 282
  • 2
  • 7
0

findViewById() is the method of the View class and you can't call it on Context object especially on Application context. So you need to call findViewById() on the View which contains your EditText. Then to get entered value you could call getText() on your EditText and put this value as a StringExtra to your Intent if you're using IntentService for example

Oleg Osipenko
  • 2,409
  • 1
  • 20
  • 28