freedom_wings 2015-11-26 08:50 采纳率: 21.1%
浏览 2116

安卓,用messenger绑定service,服务的消息发不到客户端,求大神指导

以下是ativity_main.xml:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击这里开始绑定service"/>
<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

以下是服务的java代码:
package com.example.test_service_messenger;

import android.app.Service;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.IBinder;
import android.content.Intent;

public class SelfService extends Service
{
class SelfHandler extends Handler
{
public void handleMessage(Message msg)
{
if(msg.what==101)
{ System.out.println("收到了客户端发来的消息");

         Message msgReply=Message.obtain(msg);

         msgReply.what=102;
         try{
                   msg.replyTo.send(msgReply);
                   System.out.println("已发送");}
         catch(Exception e){
             e.printStackTrace();
         }
         }
     }
 }

  Messenger messenger=new Messenger(new SelfHandler());

  public IBinder onBind(Intent intent)
  {
      return messenger.getBinder();
  }

}

以下是客户端mainActivity.java的代码:

package com.example.test_service_messenger;

import android.os.Bundle;
import android.widget.*;
import android.app.Activity;
import android.view.Menu;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.ComponentName;
import android.os.IBinder;
import android.content.Intent;
import android.content.Context;

public class MainActivity extends Activity {
Intent intent=new Intent();
Button but=null;
TextView textview=null;
Messenger messenger=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent.setClass(MainActivity.this,SelfService.class);
but=(Button)super.findViewById(R.id.button);
textview=(TextView)super.findViewById(R.id.textview);
but.setOnClickListener(new OnClickListenerimpl());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

ServiceConnection connection=new ServiceConnection()
{
public void onServiceConnected(ComponentName arg1,IBinder binder)
{

        messenger=new Messenger(binder);
   }
 public void onServiceDisconnected(ComponentName arg1)
 {

 }

};
Handler handler=new Handler()
{
public void handleMessage(Message msg)
{

       if(msg.what==102)
       {
           System.out.println("接收到了服务传回来的消息");
           MainActivity.this.textview.setText("接收到了服务传回的消息");
       }
   }

};

Messenger messenger1 =new Messenger(handler);

class OnClickListenerimpl implements OnClickListener
{
public void onClick(View v)
{
bindService(intent,connection,Context.BIND_AUTO_CREATE);
Message message=Message.obtain();
message.what=101;
message.replyTo=messenger;
try{messenger.send(message);}
catch(Exception e)
{
e.printStackTrace();
}
}
}

}

以下是logcat:
图片说明

从logcat中可看出,服务已接收到了客户端发来的消息,并已经发回了消息,但是服务那边收不到服务发回来的消息?请问这个应该怎么解决?

另外我还想问问,多个线程或进程间互发消息,线程或进程发消息是怎样设定发往哪里的?又是怎样知道是发给自己的?是广播,然后让what相等的其他线程,进程知道是发给自己的,还是怎样?顺便希望大家能指导指导一下这个问题。

  • 写回答

1条回答

  • 花前月下的细说 2015-11-26 11:32
    关注

    服务那边收不到服务发回来的消息,是不是回调写的问题。

    我也是新手,我觉得用广播好点。

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思