Activity与Service通信
来源:互联网 发布:伦佐皮亚诺 知乎 编辑:程序博客网 时间:2024/05/29 07:03
1、使用广播
2、使用ServiceConnection
采用startService启动Service:
Service会经历OnCreate然后是onStartonStartCommand(Intent intent, int flags, int startId),接着一直处于运行状态,知道stopService的时候调用onDestroy方法,如果调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行着。
如果调用Context.startService()前服务已经被创建,则多次调用startService不会导致服务被多次的创建,但会导致多次调用onstartCommand方法。
采用BindService
调用者与与服务绑定在了一起,调用者一旦退出,服务也就终止,大有不求同生但求同死
onbind()只有采用Context.bindService()方法启动服务时才会回调该方法,当调用者与服务已经绑定的时候,多次调用Context.bindService()不会导致这个方法被多次的调用。
采用 Context.bindService()方法启动服务时只能调用onUnbind()方法解除调用者与服务者,服务结束时才会调用onDestroy()方法。
不和ACtivity交互的本地服务
public class localService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("IBinder()");
return null;
}
@Override
public void onCreate() {
System.out.println("onCreate()");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("ondestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onstartCommand(0");
return super.onStartCommand(intent, flags, startId);
}
}
public class MainActivity extends Activity implements OnClickListener{
private Button startBtn,stopBtn;
private TextView tv;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn=(Button) findViewById(R.id.start);
stopBtn=(Button) findViewById(R.id.stop);
tv=(TextView) findViewById(R.id.textView1);
startBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
tv.setText("sdsdsdsd");
intent=new Intent(MainActivity.this, localService.class);
startService(intent);
System.out.println("sss");
break;
case R.id.stop:
tv.setText("");
intent=new Intent(MainActivity.this, localService.class);
stopService(intent);
System.out.println("sss1");
break;
default:
break;
}
}
}
和Activity交互的本地服务
public class localService extends Service {
private MyBinder myBinder=new MyBinder();
@Override
public IBinder onBind(Intent intent) {
System.out.println("IBinder()");
return myBinder;
}
@Override
public void onCreate() {
System.out.println("onCreate()");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("ondestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onstartCommand(0");
return super.onStartCommand(intent, flags, startId);
}
public void print(){
System.out.println("Activity与Service连接成功啦");
}
public class MyBinder extends Binder{
public localService getService(){
return localService.this;
}
}
}
public class MainActivity extends Activity implements OnClickListener{
private Button startBtn,stopBtn;
private TextView tv;
Intent intent;
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn=(Button) findViewById(R.id.start);
stopBtn=(Button) findViewById(R.id.stop);
tv=(TextView) findViewById(R.id.textView1);
startBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
tv.setText("sdsdsdsd");
intent=new Intent(MainActivity.this, localService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
break;
case R.id.stop:
tv.setText("");
if(flag==true){
unbindService(conn);
flag=false;
}
break;
default:
break;
}
}
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder binder=(MyBinder) service;
localService bindService=binder.getService();
bindService.print();
flag=true;
}
};
}
- Activity与Service通信
- Service与Activity通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Service与Activity通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Activity与Service通信
- Service与Activity通信
- Service与Activity进行通信
- (转)Activity与Service通信
- android activity与service通信
- java的super关键字和this指针
- POJ 1979-Red and Black【基础DFS】
- C语言-动态内存分配
- Java回调机制(异步)
- eclipse启动找不到javaw.exe
- Activity与Service通信
- 贪心算法——n个数连接得到最小或最大的多位整数
- Unity 协程使用指南
- BZOJ1433: [ZJOI2009]假期的宿舍
- Qt与JS相互调用
- 【NCRE】-MongoDB宏观总结
- 《剑指offer》——合并两个排序的链表
- 36项修炼:解决问题—个案突破和障碍排除
- urllib & urllib2