English 中文(简体)
在开始服务时,我如何获得IBinder/SERConnection/onserviceConnected?
原标题:How do I get an IBinder/ServiceConnection/onServiceConnected when starting a Service with startService?

在我开始使用起职服务时,我一直在寻找某种方式获得服务。

I haven t found a way, so I have been searching a bit and found this: Does each Activity need to Bind to a Service & What happens when it was created with startService()

在那里,共同认识指出,如果我在开始服务呼吁之后对服务进行约束,就没有问题了。

因此,我认为,我先开始服务(......),然后直接接过服务约束(......)(因此,有人叫服务公司)。 但后来,该局被两次处决。 或许是因为开办服务是“完成”的。

Question is: How do I get a reference to my Service (the IBinder), ie. how do I get the onServiceConnected to fire if I start my Service with startService?

------

我仍然想知道你可能提出的任何答案和想法。 我为此做了一个“hack”:

我仅作静态参考(见ef.java)。 我有公开的<代码>static IBinder myBinder = 无>和我的<编码>.onCreate。 简单明了

SRef.myBinder = myBinder;

This doesn t seem right to me, so any other ideas on how it is supposed to work would be appreciated.

问题回答

I use the exact same technique (a samba client service), onCreate is never called twice for me and I get the binder (by connection callback) as I would expect. A new start of activity doesn t fire a onCreate either because previous startService performed the startup of the service already.

这里是我的法典(可以是三维,但也许可以帮助):

活动(onCreate):

startService(new Intent(this, SambaService.class));
bindService(new Intent(this, SambaService.class), sambaServiceConnection,
        Context.BIND_AUTO_CREATE);

服务:

private ServiceBinder mServiceBinder = new ServiceBinder();

public class ServiceBinder extends Binder {
    public SambaService getService() {
        return SambaService.this;
    }
}

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签