package com.example.service_message;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
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.widget.Button;
import android.widget.TextView;
/**
* @author HD
* @date 2015-11-26
* @package_name com.example.service_message
* @file_name MainActivity.java
*/
public class MainActivity extends Activity {
TextView mCallbackTextView;
Button mButton;
Message msg;
Messenger mMessenger;
Messenger activityMessenger;
Boolean misBound;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MyService.HAND_MESSENAGER:
Log.i("hhhd", "MyService.HAND_MESSENAGER___Activity");
int arg1 = msg.arg1;
mCallbackTextView.setText("传送成功");
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCallbackTextView = (TextView) findViewById(R.id.textView1);
mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, MyService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
misBound = true;
}
});
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("hhhd", "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("hhhd", "onServiceConnected");
mMessenger = new Messenger(service);
try {
msg = new Message();
msg.what = MyService.MSG_FLAG;
mMessenger.send(msg);
Message callbackmsg = Message.obtain(handler,
MyService.REGISTER_CLIENT);
activityMessenger = new Messenger(handler);
callbackmsg.replyTo = activityMessenger;
mMessenger.send(callbackmsg);
Message handMsg = Message.obtain(null,
MyService.HAND_MESSENAGER, 0, 0);
mMessenger.send(handMsg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
protected void onDestroy() {
doUnbindService();
};
void doUnbindService(){
if(misBound){
if(mMessenger != null){
Message msg = Message.obtain(null, MyService.UNREGISTER_CLIENT, 0, 0);
try {
mMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
unbindService(conn);
misBound = false;
}
}
package com.example.service_message;
import java.util.HashSet;
import java.util.Iterator;
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.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
/**
* @author HD
* @date 2015-11-26
* @package_name com.example.service_message
* @file_name MyService.java
*/
public class MyService extends Service {
static final int MSG_FLAG = 1;
static final int REGISTER_CLIENT = 2;
static final int UNREGISTER_CLIENT = 3;
static final int HAND_MESSENAGER = 4;
static final int MSG_ARG1 = 5;
HashSet<Messenger> hashSet = new HashSet<Messenger>();
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FLAG:
Log.i("hhhd", "msg.what is emtried");
Toast.makeText(getApplicationContext(), "is Successful",
Toast.LENGTH_SHORT).show();
break;
case REGISTER_CLIENT:
Log.i("hhhd", "REGISTER_CLIENT");
hashSet.add(msg.replyTo);
break;
case UNREGISTER_CLIENT:
Log.i("hhhd", "UNREGISTER_CLIENT");
hashSet.remove(msg.replyTo);
break;
case HAND_MESSENAGER:
Log.i("hhhd", "HAND_MESSENAGER");
Iterator<Messenger> iterator = hashSet.iterator();
while (iterator.hasNext()) {
Messenger messenger = iterator.next();
Message message = Message.obtain(null, MyService.HAND_MESSENAGER, MSG_ARG1, 0);
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
default:
super.handleMessage(msg);
break;
}
}
}
@Override
public void onCreate() {
super.onCreate();
Log.i("hhhhd", "onCreate");
}
final Messenger mMessenger = new Messenger(new IncomingHandler());
@Override
public IBinder onBind(Intent intent) {
Log.i("hhhd", "onbind");
return mMessenger.getBinder();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("hhhd", "onDestroy");
}
}
<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"
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="com.example.service_message.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="100dp"
android:layout_marginTop="129dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="72dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service_message"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:process=":remote"></service>
</application>
</manifest>