Service
来源:互联网 发布:淘宝达人直播申请入口 编辑:程序博客网 时间:2024/06/10 19:36
Service和Thread的关系
Service是运行在主线程中的,所以执行一些耗时操作必须在service开一个子线程,那为什么不直接在activity开一个子线程呢?因为activity开一个子线程,如果activity被销毁了,那线程跟着完蛋,而且另外的activity无法对其线程进行操作,而service的运行时完全不需要依赖ui的,即使activity被销毁,哪怕程序被关闭,只要进程在,它都可以继续运行,而且所有activity都可以对service进行关联,
因此,使用Service来处理后台任务,Activity就可以放心地finish,完全不需要担心无法对后台任务进行控制的情况。
普通启动service
首先创建MyService继承service
重新三个方法
onCreate (服务创建时调用的)
onDestroy (服务销毁时调用的)
onStartCommand(每一次启动服务都会调用)
public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub UserLog.Print("oncreate"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub UserLog.Print("onDestroy"); super.onDestroy(); } /* (non-Javadoc) * @see android.app.Service#onStartCommand(android.content.Intent, int, int) * startId service启动的次数 */startId @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub UserLog.Print("onStartCommand"+flags+"---"+startId); return super.onStartCommand(intent, flags, startId); }}
public class MainActivity extends Activity { private Intent itIntent ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); itIntent = new Intent(this,MyService.class); } public void start(View view){ startService(itIntent); } public void stop(View view){ stopService(itIntent); }
service 和acticity的通讯 用绑定服务
可以指定service执行什么任务
service:
首先在MyService里面创建一个MyBind 类继承Binder 添加需要的操作方法,比如下载downlod();
然后创建MyBind全局变量对象
bindService启动service的时候会调用onBind 方法 ,onBind把MyBind对象return出去,给ServiceConnection 接收
Mainactivity:
创建一个匿名类ServiceConnection 实现连个方法 这两个方法分别会在Activity与Service建立关联和解除关联的时候调用
bindService()方法接收三个参数,第一个参数就是刚刚构建出的Intent对象,第二个参数是前面创建出的ServiceConnection的实例,第三个参数是一个标志位,这里传入BIND_AUTO_CREATE表示在Activity和Service建立关联后自动创建Service,这会使得MyService中的onCreate()方法得到执行,但onStartCommand()方法不会执行。
如果同时点了 startService 跟bindService :
点击Stop Service按钮只会让Service停止,点击Unbind Service按钮只会让Service和Activity解除关联,一个Service必须要在既没有和任何Activity关联又处理停止状态的时候才会被销毁。
public class MyService extends Service { private MyBind myBind = new MyBind(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return myBind; } @Override public void onCreate() { // TODO Auto-generated method stub UserLog.Print("oncreate"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub UserLog.Print("onDestroy"); super.onDestroy(); } /* (non-Javadoc) * @see android.app.Service#onStartCommand(android.content.Intent, int, int) * startId service启动的次数 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub UserLog.Print("onStartCommand"+flags+"---"+startId); return super.onStartCommand(intent, flags, startId); } class MyBind extends Binder{ public void downlod(){ System.out.println("执行下载任务"); } }}
public class MainActivity extends Activity { private Intent itIntent ; // private ServiceConnection serviceConnection =new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); itIntent = new Intent(this,MyService.class); } public void start(View view){ bindService(itIntent,serviceConnection , BIND_AUTO_CREATE); } public void stop(View view){ unbindService(serviceConnection); }}
- Service
- Service
- service
- Service
- service
- Service
- Service
- service
- Service
- Service
- Service
- service
- service
- Service
- Service
- Service
- Service
- Service
- 引导好评弹窗怎么做才能让用户体验更好?
- <p>设置字体颜色
- xcode7真机调试
- 对于为什么要使用接口的个人理解
- 太原UI设计师H5的新标签你知道有哪些吗?
- Service
- 安卓6版本中删除Apache HTTP客户端支持
- mysql5.5性能测试工具mysqlslap使用方法
- 1059. Prime Factors (25)
- Win7 禁止Ctrl+Alt+Del、Win+L等任意系统热键
- bzoj4596: [Shoi2016]黑暗前的幻想乡
- ELK 简单安装配置
- Hibernate阶段性总结(一)
- JAVA 有值类型么?