从服务获取活动的参考

时间:2011-09-04 09:46:12

标签: android android-activity android-intent android-service

我需要从服务中获取对主要活动的引用。

这是我的设计:

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
 }
}

我该怎么做?

[编辑]

感谢大家的答案! 这个应用程序是一个Web服务器,此时只适用于线程,我想使用服务,以使其在后台运行。 问题是我有一个负责从资产中获取页面的类,要执行此操作,我需要使用此方法:

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

此时我的项目只有一个Activity而没有服务,所以我可以直接从自身传递main活动的引用:

WebServer ws= new DroidWebServer(8080,this);

所以,为了让这个应用程序与服务一起工作,我应该在设计中做些什么改变?

4 个答案:

答案 0 :(得分:8)

您没有解释为什么需要这个。但这绝对是糟糕设计。存储对Activity的引用是不应该对活动做的第一件事。好吧,你可以,但你必须跟踪Activity生命周期并在调用onDestroy()后释放引用。如果您不这样做,您将收到内存泄漏(例如,配置更改时)。而且,在onDestroy()被调用之后,Activity被认为是死的,无论如何都很可能无用。

所以只是不要将引用存储在Service中。描述你需要实现的目标。我相信那里有更好的选择。


<强>更新

好的,所以你实际上并不需要引用Activity。相反,您需要引用Context(在您的情况下应该是ApplicationContext,以便不保留对Activity或任何其他组件的引用)。

假设您有一个处理WebService请求的单独类:

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); 
     }
}

现在你可以创建&amp;使用来自Service的WebService实例(建议用于此类后台任务)或甚至来自Activity(当涉及Web服务调用或长时间后台任务时,这样做更加棘手)。

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;
    }
}

答案 1 :(得分:2)

要在您的服务和活动之间进行通信,您应该使用AIDL。 有关此链接的更多信息:

编辑:(感谢Renan Malke Stigliani) http://developer.android.com/guide/components/aidl.html

答案 2 :(得分:2)

除非活动和服务分开,否则AIDL过度杀伤。

只需将活页夹用于本地服务即可。 (这里的完整示例:http://developer.android.com/reference/android/app/Service.html

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

答案 3 :(得分:0)

同意inazaruk的评论。但是,就活动和服务之间的通信而言,您有几个选择 - AIDL(如上所述),Messenger,BroadcastReicever等.Messenger方法类似于AIDL,但不要求您定义接口。你可以从这里开始:

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

相关问题