在使用bindService启动IntentService时应调用onHandleIntent吗? [英] Should onHandleIntent be called when IntentService is started with bindService?
问题描述
我的服务扩展了IntentService
,当它以startService
启动时,会调用onHandleIntent
.但是,当服务以bindService
启动(我确实需要绑定)时,不会调用onHandleIntent
.
My service extends IntentService
and when it is started with startService
, onHandleIntent
gets called. However, when the service is started with bindService
(I do need binding), onHandleIntent
does not get called.
用bindService
启动IntentService
时应调用onHandleIntent
吗? startService
是IntentService
应该启动的唯一方法吗?
Should onHandleIntent
be called when IntentService
is started with bindService
? Is startService
the only way IntentService
should be started?
IntentService
的文档显示以下内容:
The documentation for IntentService
says the following:
客户端通过startService(Intent)调用发送请求; 该服务会根据需要启动,使用工作线程依次处理每个Intent,并在工作耗尽时自行停止.
Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
当前,我通过在bindService
之后立即调用startService
解决了我的问题,但是我发现它很丑陋.我想知道有一种方法可以使它只用一个电话就可以工作.
Currently I solve my problem by calling startService
right after bindService
but I find it ugly. I would like to know is there a way to make it work with just one call.
随后是代码片段,可能是我遗漏了一些明显的东西.
Code snippets follow, it might be that I am missing something obvious.
ExampleService.java
public class ExampleService extends IntentService {
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message message) {
if (message.replyTo != null) {
outMessenger = message.replyTo;
}
}
}
private Messenger messenger = new Messenger(new IncomingHandler());
private Messenger outMessenger = null;
public ExampleService() {
super("ExampleService");
}
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
@Override
protected void onHandleIntent(Intent arg0) {
System.out.println("Service started");
for (int i = 0; i < 5; i++) {
SystemClock.sleep(5000);
if (outMessenger != null) {
try {
outMessenger.send(Message.obtain());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Service Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService">
<intent-filter>
<action android:name="com.example.service.ExampleService" />
</intent-filter>
</service>
</application>
</manifest>
MainActivity.java (调用方)
public class MainActivity extends Activity implements ServiceConnection {
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Toast.makeText(getApplicationContext(), "Message received", Toast.LENGTH_SHORT).show();
System.out.println("Message received!");
super.handleMessage(msg);
}
}
private Messenger messenger = new Messenger(new IncomingHandler());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.service.ExampleService");
bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE);
//startService(intent);
}
});
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Message message = Message.obtain();
message.replyTo = messenger;
try {
new Messenger(binder).send(message);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
推荐答案
在使用bindService启动IntentService时应调用onHandleIntent吗?
Should onHandleIntent be called when IntentService is started with bindService?
否.
startService是启动IntentService的唯一方法吗?
Is startService the only way IntentService should be started?
恕我直言,是的.恕我直言,IntentService
不是为绑定模式设计的.
IMHO, yes. IMHO, IntentService
is not designed for the binding pattern.
就您而言,您可以:
- 通过
startService()
发送的命令中的Intent
附加内容从活动中传递Messenger
,或者 - 使用
LocalBroadcastManager
,或 - 使用奥托(Otto),或
- 如果
IntentService
可能会延续到活动的整个生命周期,并且您想在这种情况下完成工作时显示Notification
,请使用有序广播, - 等等.
- Pass a
Messenger
from the activity in anIntent
extra in the command sent bystartService()
, or - Use
LocalBroadcastManager
, or - Use Otto, or
- Use an ordered broadcast, if the
IntentService
might continue past the activity's life and you want to, say, display aNotification
when the work gets done in that case, - Etc.
这篇关于在使用bindService启动IntentService时应调用onHandleIntent吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!