我的服务继承了
客户端通过
目前我通过在
以下是代码片段,可能是我忽略了什么明显的问题。 ExampleService.java
IntentService
,当使用startService
启动时,会调用onHandleIntent
。然而,当服务使用bindService
启动(我需要绑定服务)时,onHandleIntent
并不会被调用。IntentService
在使用bindService
启动时是否应该调用onHandleIntent
?startService
是唯一启动IntentService
的方式吗?
IntentService
文档中写道:客户端通过
startService(Intent)
调用发送请求;服务会按需启动,使用工作线程逐个处理每个Intent,并在没有任务时停止自身。目前我通过在
bindService
之后立即调用startService
来解决这个问题,但觉得这样做不美观。我想知道是否有办法只用一个调用就使其正常工作。以下是代码片段,可能是我忽略了什么明显的问题。 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();
}
}
}
}
}
服务清单.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
}
}