Service的浅谈
来源:互联网 发布:成都犀牛软件培训班 编辑:程序博客网 时间:2024/06/05 11:00
package com.example.fafa.justabouthandler;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;/** * Created by zhang on 2017/7/3. */public class MyService extends Service { //为了让活动和服务关联起来 借助onbind方法 把自己写的新的binder方法 用onbind返回 private DownloadBinder mbinder= new DownloadBinder(); public MyService() { } @Nullable @Override public IBinder onBind(Intent intent) { return mbinder; } @Override public void onCreate() { super.onCreate(); Log.e("TAG", "onCreate: exe" ); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("TAG", "onStartCommand: exe" ); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e("TAG", "onDestroy: exe" ); super.onDestroy(); } class DownloadBinder extends Binder{ public void satrtDownload(){ Log.e("TAG", "satrtDownload exe" ); } public int getProgress(){ Log.e("TAG", "getProgress exe"); return 0; } }}
//首先先建立一个服务 然后再建立一个活动来关联服务
package com.example.fafa.justabouthandler;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;/** * Created by zhang on 2017/7/3. */public class MyserviceActivity extends AppCompatActivity implements View.OnClickListener { private Button start; private Button stop; private Button bind; private Button unbind; private MyService.DownloadBinder downloadBinder; private ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { downloadBinder= (MyService.DownloadBinder) service; downloadBinder.satrtDownload();; downloadBinder.getProgress(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myservice); initView(); } private void initView() { start = (Button) findViewById(R.id.start_service); stop = (Button) findViewById(R.id.stop_service); bind = (Button) findViewById(R.id.bind_service); unbind = (Button) findViewById(R.id.unbind_service); start.setOnClickListener(this); stop.setOnClickListener(this); bind.setOnClickListener(this); unbind.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_service: Intent startIntent= new Intent(this,MyService.class); startService(startIntent); break; case R.id.stop_service: Intent stopIntent= new Intent(this,MyService.class); stopService(stopIntent); break; case R.id.bind_service: //绑定的时候需要一个connection 所以创建个ServiceConnection在onServiceConnected()中来指挥服务干什么 Intent bindintent=new Intent(this,MyService.class); bindService(bindintent,connection,BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; } }}//布局文件里面只有四个按钮
//关于服务的声明周期 一旦调用startservice()方法 服务就会启动起来并且回调onStartCommand()
//方法服务启动后会一直保持运行状态直到停止服务或者第on工了stopself()方法虽然每调用一次启动方
法onStartCommand就会执行一次 但是只要调用一次停止就行了,如果同时启动了服务和绑定了服务那么
就要同时点击停止服务和解绑 服务才回销毁
阅读全文
0 0
- Service的浅谈
- 浅谈Service
- 浅谈Service
- 浅谈Service
- 浅谈Service
- 浅谈Android Service与IntentService的区别
- 浅谈对Android Service 的理解
- 浅谈web service
- Web Service 浅谈
- 浅谈Android Service 组件
- 浅谈Android Service 组件
- android service浅谈
- 浅谈android 之 service
- Web Service 浅谈
- android 浅谈service
- 浅谈Android Service 组件
- 浅谈Web Service
- 浅谈 Android Service
- Perl语言面向对象入门
- 自学的程序员如何找到好工作?
- 谈谈 iOS 中图片的解压缩
- [C++ 从入门到放弃-05]C++STL之map映照容器
- Android面试复习(Android篇二)
- Service的浅谈
- 你距离行业大师还有多远的路(下)
- Android Environment类
- Ajax入门基础01
- mysql处理海量数据时的一些优化查询速度方法
- Android联合第三方实现使用TextToSpeech实现语音朗读的功能
- list去重
- 使用DroidPlugin实现模块化开发(主要说调用)
- MySQL运维之神奇的参数(终结篇)