开发者

get Activity's reference from a Service

I need to get a reference to the main Activity from a Service.

This is my design:

MainActivity.java

public class MainActivity extends Activity{
private Intent myIntent;
onCreate(){
 myIntent=new Intent(MainActivity.this, MyService.class);

 btnStart.setOnClickListener(new OnClickListener(){
  public void onClick(View V){
   startService(myIntent);
   });
}}

MyService.java

class MyService extends Service{

 public IBinder onBind(Intent intent) {
  return null;
 }

 onCreate(){
 //Here I need to have a MainActivity reference
 //to pass it to another object
 }
}

How can I do this?

[EDIT]

Thanks to all for the answers! This app is a web serv开发者_运维百科er, that at this moment works only with threads, and I want to use a service instead, to make it work also in the background. The problem is that I have a class that is responsible for getting the page from assets, and to do this operation I need to use this method:

InputStream iS =myActivity.getAssets().open("www/"+filename); 

At this moment my project has only one Activity and no services, so I can pass the main activity's reference directly from itself:

WebServer ws= new DroidWebServer(8080,this);

So, in order to make this app work with a service, what should I change in my design?


You didn't explain why you need this. But this is definitely bad design. Storing references to Activity is the first thing you shouldn't do with activities. Well, you can, but you must track Activity lifecycle and release the reference after its onDestroy() is called. If you are not doing this, you'll get a memory leak (when configuration changes, for example). And, well, after onDestroy() is called, Activity is considered dead and is most likely useless anyway.

So just don't store the reference in Service. Describe what you need to achieve instead. I'm sure there are better alternatives out there.


UPDATE

Ok, so you do not actually need reference to Activity. Instead you need reference to Context (which in your case should be ApplicationContext to not keep reference to Activity or any other component for that matter).

Assuming you have a separate class that handles WebService request:

class WebService 
{   
     private final Context mContext;
     public WebService(Context ctx) 
     {
        //The only context that is safe to keep without tracking its lifetime
        //is application context. Activity context and Service context can expire
        //and we do not want to keep reference to them and prevent 
        //GC from recycling the memory.
        mContext = ctx.getApplicationContext(); 
     }

     public void someFunc(String filename) throws IOException 
     {
         InputStream iS = mContext.getAssets().open("www/"+filename); 
     }
}

Now you can create & use WebService instance from Service (which is recommended for such background tasks) or even from Activity (which is much trickier to get right when web service calls or long background tasks are involved).

An example with Service:

class MyService extends Service
{
    WebService mWs;
    @Override
    public void onCreate()
    {
        super.onCreate();
        mWs = new WebService(this);

       //you now can call mWs.someFunc() in separate thread to load data from assets.
    }

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


To communicate between your service and activity you should use AIDL. More info on this link:

EDIT: (Thanks Renan Malke Stigliani) http://developer.android.com/guide/components/aidl.html


The AIDL is overkill unless the activity and service are in seperate apks.

Just use a binder to a local service. (full example here: http://developer.android.com/reference/android/app/Service.html)

public class LocalBinder extends Binder {
        LocalService getService() {
            return LocalService.this;
        }
    }


Agree with inazaruk's comments. But, In terms of communicating between an Activity and a Service, you have a few choices - AIDL (as mentioned above), Messenger, BroadcastReicever, etc. The Messenger method is similar to AIDL but doesn't require you to define the interfaces. You can start here:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜