Activity通过获得Service实例与其进行通信

来自:http://blog.sina.com.cn/s/blog_70677d110100yfyk.html

 要用到Binder和ServiceConnection类,


借用下别人的例子,这个例子写的简单易懂:

 

第一步:新建一个Android工程,我这里命名为ServiceDemo.

第二步:修改main.xml代码,我这里增加了四个按钮,代码如下:

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

android:id="@+id/text"  

   android:layout_width="fill_parent" 

   android:layout_height="wrap_content" 

   android:text="@string/hello"

   />

android:id="@+id/startservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="startService"

/>

android:id="@+id/stopservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="stopService"

/>

android:id="@+id/bindservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="bindService"

/>

android:id="@+id/unbindservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="unbindService"

/>

第三步:新建一个Service,命名为MyService.java代码如下:
package yt.hy.test;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;

public class MyService extends Service {
private static final String TAG = "MySerice";
private MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "start IBinder"); 
return mBinder;
}
@Override
public void onCreate() {
Log.e(TAG, "start onCreate");
super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
Log.e(TAG, "start onStart");
super.onStart(intent, startId);
}

@Override
public void onDestroy() {
Log.e(TAG, "start onDestroy");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "start onUnbind");
return super.onUnbind(intent);
}

// 返回时间
public String getSystemTime() {
Time t = new Time();
t.setToNow();
return t.toString();
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}

}
第四步:修改ServiceDomeActivity.java,代码如下:
package yt.hy.test;

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.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ServiceDomeActivity extends Activity implements OnClickListener {

private MyService myService;
private TextView textView;
private Button startServiceButton;
private Button stopServiceButton;
private Button bindServiceButton;
private Button unbindServiceButton;
private Context mContext;

private ServiceConnection mServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.MyBinder) service).getService();
textView.setText("i am from service:" + myService.getSystemTime());
}

@Override
public void onServiceDisconnected(ComponentName name) {

}

};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}

private void setupViews() {
mContext = ServiceDomeActivity.this;
textView = (TextView) findViewById(R.id.text);
startServiceButton = (Button) findViewById(R.id.startservice);
stopServiceButton = (Button) findViewById(R.id.stopservice);
bindServiceButton = (Button) findViewById(R.id.bindservice);
unbindServiceButton = (Button) findViewById(R.id.unbindservice);

startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v == startServiceButton) {
Intent i = new Intent();
i.setClass(ServiceDomeActivity.this, MyService.class);
mContext.startService(i);
} else if (v == stopServiceButton) {
Intent i = new Intent();
i.setClass(ServiceDomeActivity.this, MyService.class);
mContext.stopService(i);
} else if (v == bindServiceButton) {
Intent i = new Intent();
i.setClass(ServiceDomeActivity.this, MyService.class);
mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
} else {
mContext.unbindService(mServiceConnection);
}
}

}
还有不要忘了在配置文件中写入service。

第六步:执行上述工程,效果图如下:

点击startServie按钮时先后执行了Service中onCreate()->onStart()这两个方法,打开Logcat视窗效果如下图:

我们这时可以按HOME键进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下:

点击stopService按钮时,Service则执行了onDestroy()方法,效果图如下所示:

这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder()方法,以及TextView的值也有所变化了,如下两张图所示:

 

最后点击unbindService按钮,则Service执行了onUnbind()方法,如下图所示:

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值