直接上代码。
布局:
<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=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Service类:
package com.example.servicedemo_v4;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class Myservice_v4 extends Service
{
private Messenger mMessenger;
private MyBinder_v4 binder_v4;
private boolean isResume = true;
private int mCount = 1;
private int nCount = 10;
@Override
public IBinder onBind(Intent intent)
{
return binder_v4;
}
@Override
public void onCreate()
{
super.onCreate();
binder_v4 = new MyBinder_v4();
thread.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
isResume = false;
}
class MyBinder_v4 extends Binder
{
public void setMessenger(Messenger messenger)
{
mMessenger = messenger;
}
public int getCount()
{
return mCount;
}
public void setFlag(boolean b)
{
isResume = b;
}
}
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
while (isResume)
{
try
{
sendObj(MainActivity.NUMONE, nCount += 10);
Thread.sleep(3000);
} catch (InterruptedException e)
{
e.printStackTrace();
Log.e("", e.toString());
}
}
}
});
private void sendObj(int numone, Object object)
{
if (mMessenger != null)
{
try
{
mCount += 1;
mMessenger.send(Message.obtain(null, numone, object));
} catch (RemoteException e)
{
Log.e("error", e.toString());
mMessenger = null;
e.printStackTrace();
}
} else
{
Log.i("", "mMessenger为空");
}
}
}
Activity类:
package com.example.servicedemo_v4;
import com.example.servicedemo_v4.Myservice_v4.MyBinder_v4;
import android.app.Activity;
import android.content.ComponentName;
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.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private boolean isBinder;
private Intent service;
private Myservice_v4.MyBinder_v4 binder_v4 = null;
private Messenger messenger;
private MyHanler mHandler;
private TextView mTv;
public static final int NUMONE = 1;
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
isBinder = false;
if (binder_v4 != null)
{
binder_v4 = null;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
Log.d("", "建立连接");
isBinder = true;
binder_v4 = (MyBinder_v4) service;
binder_v4.setMessenger(messenger);
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.tv);
service = new Intent(this, Myservice_v4.class);
mHandler = new MyHanler();
messenger = new Messenger(mHandler);
startService(service);
bindService(service, conn, BIND_AUTO_CREATE);
}
class MyHanler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case NUMONE:
Log.d("", "执行了handler");
int nCount = (Integer) msg.obj;
mTv.setText("mCount=" + binder_v4.getCount() + "--nCount="
+ nCount);// 可以通过binder_v4提供的方法得到service中变量的值,也能直接接收obj的值。
Log.d("", "mCount=" + binder_v4.getCount() + "--nCount="
+ nCount);
break;
default:
super.handleMessage(msg);
break;
}
}
}
@Override
protected void onDestroy()
{
Log.d("", "onDestroy()");
super.onDestroy();
if (isBinder)
{
stopService(service);
unbindService(conn);
}
}
}
还是要提醒下,不要忘了注册service:
<service android:name="com.example.servicedemo_v4.Myservice_v4" >
</service>
运行一阵儿,退出activity,看下log:
上述为service向activity发送数据,如果需要activity向service发送数据,思路也是一样的哦。