问题描述
我已经创建了一个既服务和需要能够互相沟通的活动。该活动应该将消息发送到服务,而服务应该以结果答复。
问题是,消息似乎并没有被做它的服务。这是在输出到日志从code:
02-07 18:26:37.057:I /任务定时器(8850):服务绑定
02-07 18:26:37.097:V /任务定时器(8850):服务连接:ComponentInfo {com.gawdl3y.android.tasktimer / com.gawdl3y.android.tasktimer.TaskService}
02-07 18:26:37.097:I /任务定时器(8850):活动发送消息:{=什么时候1 = -1d11h33m57s160ms}
02-07 18:26:37.167:I / ActivityManager(482):显示com.gawdl3y.android.tasktimer / .TaskListActivity:+ 166ms
从其他应用程序没有被记录下来。
这里是我的相关code:
TaskService.java
公共类TaskService延伸服务{
公共静态字符串标记= NULL; 私人最终的IBinder粘结剂=新的ServiceBinder();
私人信使信使=新使者(新IncomingHandler()),activityMessenger; @覆盖
公共无效的onCreate(){
Log.i(的getString(R.string.app_name),服务启动);
TAG =的getString(R.string.service_label);
} @覆盖
公众的IBinder onBind(意向意图){
返回粘结剂;
} @覆盖
公共无效的onDestroy(){
Log.i(TAG,服务破坏);
} 公共类的ServiceBinder扩展粘结剂{
TaskService的getService(){
返回TaskService.this;
}
} 私人final类IncomingHandler扩展了Handler {
@覆盖
公共无效的handleMessage(消息MSG){
activityMessenger = msg.replyTo;
Log.i(TAG,服务收到的消息:+味精); 开关(msg.what){
案例TaskListActivity.MSG_GET_TASKS:
//创建响应消息,并将其发送
消息响应= Message.obtain(NULL,1,任务);
尝试{
activityMessenger.send(响应);
Log.i(TAG,服务发送的消息:+响应);
}赶上(android.os.RemoteException E){
Log.i(TAG,服务无法发送消息:+响应+(+ e.getLocalizedMessage()+所造成的+ e.getCause()+));
} //返回消息到全局池
response.recycle(); 打破;
默认:
super.handleMessage(MSG);
}
}
}
}
TaskListActivity.java
公共类TaskListActivity扩展SherlockActivity { 公共静态最终诠释MSG_GET_TASKS = 1; 私人TaskService服务;
私人信使信使=新使者(新IncomingHandler()),serviceMessenger;
连接= FALSE私人布尔; 私人ServiceConnection serviceConnection =新ServiceConnection(){
公共无效onServiceConnected(组件名名称,服务的IBinder){
Log.v(TAG,服务连接:+姓名); //设置服务Messenger和连接状态
TaskListActivity.this.serviceMessenger =新使者(服务);
TaskListActivity.this.connected = TRUE; //创建一个新的消息发送给检索任务
消息味精= Message.obtain(NULL,MSG_GET_TASKS);
msg.replyTo =使者; //发送消息
尝试{
serviceMessenger.send(MSG);
Log.i(TAG活动发送的消息:+味精);
}赶上(android.os.RemoteException E){
Log.i(TAG活动未能发送的消息:+味精+(+ e.getLocalizedMessage()+所造成的+ e.getCause()+));
} //返回消息到全局池
msg.recycle();
} 公共无效onServiceDisconnected(组件名名){
Log.v(TAG,服务断开连接:+姓名); //重置服务信使和连接状态
TaskListActivity.this.serviceMessenger = NULL;
TaskListActivity.this.connected = FALSE;
}
}; @覆盖
保护无效调用onStart(){
super.onStart(); //显示负载指示
// setSupportProgressBarIndeterminateVisibility(真); //开始并绑定服务
意向意图=新意图(这一点,TaskService.class);
startService(意向);
如果(bindService(意向,serviceConnection,Context.BIND_ABOVE_CLIENT | Context.BIND_ADJUST_WITH_ACTIVITY)){
Log.i(TAG,服务绑定);
}其他{
Log.i(TAG,服务不绑定);
Toast.makeText(这一点,任务服务无法绑定,Toast.LENGTH_SHORT).show();
完();
}
} @覆盖
保护无效的onStop(){
super.onStop(); //取消绑定服务
unbindService(serviceConnection);
Log.i(TAG,服务绑定);
} 私人final类IncomingHandler扩展了Handler {
@覆盖
公共无效的handleMessage(消息MSG){
开关(msg.what){
案例MSG_GET_TASKS:
Log.i(TAG活动收到的消息:+味精);
onTasksLoaded((ArrayList的<任务>)msg.obj);
打破;
默认:
super.handleMessage(MSG);
}
}
}
}
在您的onBind方法,你可以尝试返回,而不是粘结剂messenger.getBinder()?
I have created both a Service and an Activity that need to be able to communicate with each other. The Activity should send a Message to the Service, and the Service should reply with a result.
The problem is that the Message doesn't seem to be making it to the Service. This is the output to the log from the code:
02-07 18:26:37.057: I/Task Timer(8850): Service bound
02-07 18:26:37.097: V/Task Timer(8850): Service connected: ComponentInfo{com.gawdl3y.android.tasktimer/com.gawdl3y.android.tasktimer.TaskService}
02-07 18:26:37.097: I/Task Timer(8850): Activity sent message: { what=1 when=-1d11h33m57s160ms }
02-07 18:26:37.167: I/ActivityManager(482): Displayed com.gawdl3y.android.tasktimer/.TaskListActivity: +166ms
Nothing else from the app is logged.Here's my relevant code:
TaskService.java
public class TaskService extends Service {
public static String TAG = null;
private final IBinder binder = new ServiceBinder();
private Messenger messenger = new Messenger(new IncomingHandler()), activityMessenger;
@Override
public void onCreate() {
Log.i(getString(R.string.app_name), "Service started");
TAG = getString(R.string.service_label);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
Log.i(TAG, "Service destroyed");
}
public class ServiceBinder extends Binder {
TaskService getService() {
return TaskService.this;
}
}
private final class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
activityMessenger = msg.replyTo;
Log.i(TAG, "Service received message: " + msg);
switch(msg.what) {
case TaskListActivity.MSG_GET_TASKS:
// Create the response message and send it
Message response = Message.obtain(null, 1, tasks);
try {
activityMessenger.send(response);
Log.i(TAG, "Service sent message: " + response);
} catch(android.os.RemoteException e) {
Log.i(TAG, "Service failed to send message: " + response + " (" + e.getLocalizedMessage() + " caused by " + e.getCause() + ")");
}
// Return the message to the global pool
response.recycle();
break;
default:
super.handleMessage(msg);
}
}
}
}
TaskListActivity.java
public class TaskListActivity extends SherlockActivity {
public static final int MSG_GET_TASKS = 1;
private TaskService service;
private Messenger messenger = new Messenger(new IncomingHandler()), serviceMessenger;
private boolean connected = false;
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "Service connected: " + name);
// Set the service messenger and connected status
TaskListActivity.this.serviceMessenger = new Messenger(service);
TaskListActivity.this.connected = true;
// Create a new message to send to retrieve the tasks
Message msg = Message.obtain(null, MSG_GET_TASKS);
msg.replyTo = messenger;
// Send the message
try {
serviceMessenger.send(msg);
Log.i(TAG, "Activity sent message: " + msg);
} catch(android.os.RemoteException e) {
Log.i(TAG, "Activity failed to send message: " + msg + " (" + e.getLocalizedMessage() + " caused by " + e.getCause() + ")");
}
// Return the message to the global pool
msg.recycle();
}
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "Service disconnected: " + name);
// Reset the service messenger and connection status
TaskListActivity.this.serviceMessenger = null;
TaskListActivity.this.connected = false;
}
};
@Override
protected void onStart() {
super.onStart();
// Show the loading indicator
//setSupportProgressBarIndeterminateVisibility(true);
// Start and bind the service
Intent intent = new Intent(this, TaskService.class);
startService(intent);
if(bindService(intent, serviceConnection, Context.BIND_ABOVE_CLIENT | Context.BIND_ADJUST_WITH_ACTIVITY)) {
Log.i(TAG, "Service bound");
} else {
Log.i(TAG, "Service not bound");
Toast.makeText(this, "Task Service couldn't be bound", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
protected void onStop() {
super.onStop();
// Unbind service
unbindService(serviceConnection);
Log.i(TAG, "Service unbound");
}
private final class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MSG_GET_TASKS:
Log.i(TAG, "Activity received message: " + msg);
onTasksLoaded((ArrayList<Task>) msg.obj);
break;
default:
super.handleMessage(msg);
}
}
}
}
In your onBind method, can you try returning the messenger.getBinder() instead of the binder?
这篇关于没有收到来自活动消息的Android服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!