behzad razzaqi
behzad razzaqi

Reputation: 93

Android service change activity imageview image

i write simple application,in the my activity i call the service with this method:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsactivity);
        Bundle extras = getIntent().getExtras();
        value= extras.getString("myid");
        Intent intent = new Intent(this, newsservice.class);
        intent.putExtra("behid", value);
        startService(intent);

    }

and in the my service fetch the image url from server,and i want Load this image on the activity ImageView,my service code is:

public class newimageService extends Service {
    String Behid;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Behid= intent.getStringExtra("behid");

        new HttpAsyncTask().execute("http://sample.org/shownewsimage.aspx");
        //in this part show the image into image view
        this.stopSelf();

        return Service.START_FLAG_REDELIVERY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


how can i run the findViewByid(R.imageView) into this service method?

Upvotes: 0

Views: 902

Answers (2)

user3906057
user3906057

Reputation:

You need to set Broadcast Receiver after image successfully downloaded. Register broadcast receiver in you activity and do update your image view. Or You can simply create Async task only. No need to create service.

Upvotes: 2

Ankur Samarya
Ankur Samarya

Reputation: 229

My 1st instinct is that you should instead have the Activity bind to your service and handle the UI update on its side instead of the Service directly modifying the Activity.

See more info here: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

And an example here: Example: Communication between Activity and Service using Messaging

Upvotes: -1

Related Questions