xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.testmessenger.MainActivity" > <Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="绑定" /> <Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="发送数据" /> <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="加法" /> </RelativeLayout>
布局
MainActivity:
package com.zzw.testmessenger; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {
Button bind, send, start;
ServiceConnection sc;
Messenger sender; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bind = (Button) findViewById(R.id.bind);
bind.setOnClickListener(this);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(this);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bind:
bindService();
break;
case R.id.send:
sendMessageToService();
break;
case R.id.start:
useStartService();
break;
}
} private void bindService() {
sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
sender = new Messenger(service);
}
}; Intent intent = new Intent(MainActivity.this, MessengerSerivice.class);
bindService(intent, sc, BIND_AUTO_CREATE);
} private void sendMessageToService() {
try {
Message msg = new Message();
int a = (int) (Math.random() * 20);
int b = (int) (Math.random() * 20);
int s[] = { a, b };
msg.obj = s;
sender.send(msg);
Log.d("sendMessageToService", "发送成功----->");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private void useStartService() {
Intent intent = new Intent(MainActivity.this, MessengerSerivice.class);
startService(intent);
} @Override
protected void onDestroy() {
unbindService(sc);
Intent intent = new Intent(MainActivity.this, MessengerSerivice.class);
stopService(intent);
super.onDestroy();
}
}
MessengerSerivice:
package com.zzw.testmessenger; import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log; public class MessengerSerivice extends Service {
private Messenger messenger;
Handler handler;
int a, b, sum; @Override
public void onCreate() {
Log.d("MessengerSerivice", "onCreate");
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int[] s = (int[]) msg.obj;
a = s[0];
b = s[1];
Log.d("收到的msg", "a=" + a + " b=" + b);
}
};
messenger = new Messenger(handler);
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MessengerSerivice", "onStartCommand");
sum = a + b;
Log.d("在onStartCommand中算出的和", sum + "");
return super.onStartCommand(intent, flags, startId);
} @Override
public IBinder onBind(Intent intent) {
Log.d("MessengerSerivice", "onBind");
return messenger.getBinder();
} }