获得活动的从服务引用 [英] get Activity's reference from a Service

查看:100
本文介绍了获得活动的从服务引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个服务引用的主要活动。

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

这是我设计的:

MainActivity.java

public class MainActivity extendsActivity{
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?

感谢所有的答案! 这个程序是一个Web服务器,那在这一刻只适用于螺纹,我想用一个服务,而不是,使其也在后台工作。 问题是,我有一个类,它是负责获得资产的页面,并要做到这一点的操作,我需要用这个方法:

Thanks to all for the answers! This app is a web server, that at this moment works only with threads, and I want to use a service instead, to make it work also in background. The problem is that I've a class that is responsible to get 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 works with a service, what should I change in my design?

推荐答案

您也没有解释为什么你需要这个。但是,这是绝对的的设计。存储引用到活动为您的第一件事情不应该做的活动。嗯,你可以,但你必须跟踪活动的生命周期和的onDestroy()被调用后的释放参考。如果你不这样做,你会得到内存泄漏(当配置更改,例如)。而且,还有,以后的onDestroy()被称为活动被认为是死的,是最有可能并没有意义。

You didn't explained 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 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.

更新

好了,你实际上并不需要提及的活动。相反,你需要参考上下文(在你的情况应该ApplicationContent不保持引用到活动或任何其他成分为此事)。

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

假设你有一个处理的WebService要求单独的类:

Assuming you have 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); 
     }
}

现在,您可以创建和放大器;使用WebService的情况下从服务(推荐这样的后台任务),或者甚至是从活动(这是更棘手马上当Web服务调用或长的后台任务的时候)。

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).

服务的例子

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

这篇关于获得活动的从服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆