package com.example.yabushan.servicetest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private EditText tx;
private MyService.Binder binder=null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx= (EditText) findViewById(R.id.editText);
textView= (TextView) findViewById(R.id.textView);
findViewById(R.id.bindService).setOnClickListener(this);
findViewById(R.id.unbindService).setOnClickListener(this);
findViewById(R.id.syncData).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bindService:
bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);
break;
case R.id.unbindService:
unbindService(this);
break;
case R.id.syncData:
if(binder!=null)
System.out.println("同步数据服务");
binder.setData(tx.getText().toString());
break;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("绑定服务");
binder = (MyService.Binder) service;
binder.getService().setCallback(new MyService.CallBack() {
@Override
public void onDataChange(String data) {
Message msg=new Message();
Bundle bundle=new Bundle();
bundle.putString("data",data);
msg.setData(bundle);
handler.sendMessage(msg);
}
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textView.setText(msg.getData().getString("data"));
}
};
}
package com.example.yabushan.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private String data="默认值";
private boolean running=true;
public MyService() {
}
//接口成员
private CallBack callback=null;
//get方法
public CallBack getCallback() {
return callback;
}
//set方法
public void setCallback(CallBack callback) {
this.callback = callback;
}
@Override
public IBinder onBind(Intent intent) {
return new Binder();
}
//用于service与activity之间沟通的桥梁
public class Binder extends android.os.Binder{
public void setData(String data){
MyService.this.data=data;
}
public MyService getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
new Thread(){
@Override
public void run() {
int i=0;
while(running) {
super.run();
i++;
String str=i + ":" + data;
System.out.println(str);
if(callback!=null){
callback.onDataChange(str);
}
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("取消绑定");
running=false;
}
//创建一个接口用来回调
public static interface CallBack{
void onDataChange(String data);
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="默认值" android:id="@+id/editText" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" android:id="@+id/bindService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消绑定服务" android:id="@+id/unbindService" /> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="同步数据" android:id="@+id/syncData" /> </LinearLayout>
-22 05:26:13.729 3767-3767/? I/System.out: 绑定服务
11-22 05:26:13.732 3767-3798/? I/System.out: 1:默认值
11-22 05:26:15.746 3767-3798/? I/System.out: 2:默认值
11-22 05:26:17.756 3767-3798/? I/System.out: 3:默认值
11-22 05:26:19.767 3767-3798/? I/System.out: 4:默认值
11-22 05:26:20.213 1554-1554/? I/LatinIME: Starting input. Cursor position = 0,0
11-22 05:26:20.277 3767-3782/? W/EGL_emulation: eglSurfaceAttrib not implemented
11-22 05:26:20.277 3767-3782/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad91ad20, error=EGL_SUCCESS
11-22 05:26:20.322 1554-3574/? W/EGL_emulation: eglSurfaceAttrib not implemented
11-22 05:26:20.323 1554-3574/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf8fe2a0, error=EGL_SUCCESS
11-22 05:26:21.776 3767-3798/? I/System.out: 5:默认值
11-22 05:26:23.786 3767-3798/? I/System.out: 6:默认值
11-22 05:26:24.229 3767-3782/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab9444c0
11-22 05:26:25.151 1554-1696/? I/LatinIME:LogUtils: Dictionary info: dictionary = spellcheck_contacts.en_US ; version = 1448169985 ; date = ?
11-22 05:26:25.152 1304-1316/? I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-22 05:26:25.163 1554-1696/? I/LatinIME:LogUtils: Dictionary info: dictionary = spellcheck_userunigram.en_US ; version = 1448169985 ; date = ?
11-22 05:26:25.173 1554-1696/? I/LatinIME:LogUtils: Dictionary info: dictionary = main:en ; version = 54 ; date = 1414726273
11-22 05:26:25.796 3767-3798/? I/System.out: 7:默认值
11-22 05:26:26.642 3767-3767/? I/System.out: 同步数据服务
11-22 05:26:27.806 3767-3798/? I/System.out: 8:默认值ww
11-22 05:26:29.815 3767-3798/? I/System.out: 9:默认值ww
11-22 05:26:31.826 3767-3798/? I/System.out: 10:默认值ww
11-22 05:26:33.783 3767-3767/? I/System.out: 同步数据服务
11-22 05:26:33.836 3767-3798/? I/System.out: 11:默认值wwq
11-22 05:26:35.846 3767-3798/? I/System.out: 12:默认值wwq
11-22 05:26:37.857 3767-3798/? I/System.out: 13:默认值wwq
11-22 05:26:39.867 3767-3798/? I/System.out: 14:默认值wwq
11-22 05:26:41.876 3767-3798/? I/System.out: 15:默认值wwq
11-22 05:26:43.886 3767-3798/? I/System.out: 16:默认值wwq
11-22 05:26:45.562 3767-3767/? I/System.out: 取消绑定