service通知activity
- 发广播
- 回调(只能是同进程,如果是在service的子线程里调用的话,那只能更新数据,无法更新界面,当然ProgressBar除外),可参考 http://blog.csdn.net/xiaanming/article/details/9750689 主要是通过getService来获取service实例,然后在service内部设置一个回调。
activity获取service数据
- bind获取,见后文例子
activity向service传数据
startService intent
activity获取service数据例子
Service中提供Binder 接口,使得activity可以获取到service中的数据,activity通过ServiceConnection的onServiceConnected获取到binder,再通过bind获取到service里的数据
代码(module:ServiceDemo)
TimeCountService内部有个线程,实现计时功能。activity获取到service里的bind实例,然后调用bind的方法获取数据。
MainActivity代码
public class MainActivity extends Activity {
private final String TAG = "main";
Button bind, unbind, getServiceStatus;
TimeCountService.MyBinder binder;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "--Service Disconnected--");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "--Service Connected--");
// 取得Service对象中的Binder对象
binder = (TimeCountService.MyBinder) service;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 绑定服务到当前activity中
Intent intent = new Intent();
// 指定开启服务的action
intent.setAction("com.time.count");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 解除绑定
binder=null;
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(binder!=null)
{
// 通过绑定服务传递的Binder对象,获取Service暴露出来的数据
Toast.makeText(MainActivity.this,
"Service.Count=" + binder.getCount(),
Toast.LENGTH_SHORT).show();
Log.i(TAG, "Service.Count=" + binder.getCount());
}
else
{
Toast.makeText(MainActivity.this,
"you need bind it",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
TimeCountService代码
package com.example.service.demo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class TimeCountService extends Service {
private final static String TAG = "main";
private int count;
private boolean quit;
private Thread thread;
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder {
// 声明一个方法,获取TimeCountService里的count值
public int getCount() {
return count;
}
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service is Created");
thread = new Thread(new Runnable() {
@Override
public void run() {
// 每间隔一秒count加1 ,直到quit为true
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
});
thread.start();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Service is Unbinded");
return true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service is started");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service is Destroyed");
this.quit = true;
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Service is Binded");
return binder;
}
}
service 在manifest内代码:
<service android:name="com.example.service.demo.TimeCountService" >
<intent-filter >
<action android:name="com.time.count" />
</intent-filter>
</service>
绑定服务时函数调用顺序为
onCreate
onBind
onServiceConnected