开发者

Android, start service using thread and warn activity when it's done

开发者 https://www.devze.com 2023-04-11 09:52 出处:网络
I have a service which has a method that downloads an image from an URL and returns an Uri. That service will get more complex when it has all the intended features. Therefore,

I have a service which has a method that downloads an image from an URL and returns an Uri. That service will get more complex when it has all the intended features. Therefore, I'm invoking its methods within a thread.

My problem is how to warn the activity开发者_如何学C that the service has done it's work.

I could change a class isFinished variable but the activity had to be constantly checking for its value.

I just want the service to tell the activity that it's work is done and the resources are available for use.

I thought something in the lines of the service calling stopSelf() and the activity was warned through "onServiceDisconnected" but that didn't seem very "political correct".

Thanks in advance


There are two ways to do it. 1. You can start your activity using by firing an intent. 2. You can Broadcast an intent and write receiver for it in your app when your receiver receives intent and onreceive method is called in this method you can start your activity using intent.

cheers...


public class MyActivity extends Activity{

    public MyActivity() {
        ...
        MyThread thread = new MyThread(this);
        thread.start();
    }

    public void onFinishedThread(...) {

    }
}

class MyThread extends Thread {

    MyActivity activity;

    public MyThread(MyActivity activity) {
        this.activity = activity;
    }

    public void run() {
        // do work
        ...
        this.activity.onFinishedThread(...);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消