Service的使用(一)
来源:互联网 发布:编程入门书籍下载 编辑:程序博客网 时间:2024/03/29 21:11
Service,是android的四大组件之一,没有界面,优先级高于Activity,运行在后台,执行一些耗时的操作,比如播放音乐,执行数据的加载,请求。
注意的是:运行在主线程,不能用它来做耗时的请求或者动作可以在服务中开一个线程,在线程中做耗时动作
Service的启动与绑定:
工程概况:
布局只有四个按钮,不列:
清单文件的声明:
MainActivity:
package ivo_chuanzhi.service;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends ActionBarActivity implements View.OnClickListener{ private Intent intent; ServiceConnection conn = new ServiceConnection() { //服务被绑定成功的时候回调 @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("绑定成功"); } //被绑定的服务断线时候会回调 @Override public void onServiceDisconnected(ComponentName name) { System.out.println("绑定失败"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //操作的是同一个Service,不管是一个Intent实例启动还是两个Intent实例 intent = new Intent(MainActivity.this, MyService.class); Button btnStart = (Button) findViewById(R.id.btnStartService); Button btnStop = (Button) findViewById(R.id.btnStopService); Button btnBind = (Button) findViewById(R.id.btnBindService); Button btnUnBind = (Button) findViewById(R.id.btnUbBindService); btnStart.setOnClickListener(this); btnStop.setOnClickListener(this); btnBind.setOnClickListener(this); btnUnBind.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnStartService: //启动服务:1、startServicer 2、绑定服务 startService(intent); break; case R.id.btnStopService: //停止服务 stopService(intent); break; case R.id.btnBindService: //绑定服务 bindService(intent, conn, Context.BIND_AUTO_CREATE); break; case R.id.btnUbBindService: //解除绑定 unbindService(conn); break; } }}
package ivo_chuanzhi.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class MyService extends Service { private boolean ServicerRunning = true; public MyService() { } //Service都必须实现的方法 @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //没绑定的话 //throw new UnsupportedOperationException("Not yet implemented"); //如果实现了绑定了,需要实现onBind接口,返回一个Binder对象,通过Binder可以实现与绑定的activity通讯 return new Binder();//可以重写Binder,方便业务与activity通讯 } //在activity里面启动了startService(new Intent(MainActivity.this, MyService.class)); //的方法都会回调这个函数,可重复执行 @Override public int onStartCommand(Intent intent, int flags, int startId) { /* new Thread(){ @Override public void run() { super.run(); while(true) { System.out.println("服务在运行。。。"); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start();*/ System.out.println(); return super.onStartCommand(intent, flags, startId); } //创建服务后第一个回调该函数,只执行一次 @Override public void onCreate() { super.onCreate(); System.out.println("我的服务被创建了"); new Thread(){ @Override public void run() { super.run(); while(ServicerRunning) { System.out.println("服务在运行。。。"); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } //stopService被执行后会回调 @Override public void onDestroy() { //stopService和UnBind都会关掉服务 System.out.println("我的服务被关掉了"); ServicerRunning = false; }}
源码里面的注释已经很详细的,在此就不再赘述
谢谢
0 0
- Service的使用(一)
- Service的使用(一)
- Service的使用(一)
- Service的使用一
- android service的使用(一)
- Android—Service的使用(一)
- Service基础(一)---》使用服务Service
- Android使用binder访问service的方式(一)
- -----------------------------Android使用binder访问service的方式(一)
- Android使用binder访问service的方式(一)
- Android使用binder访问service的方式(一)
- Android:Service(一)——Service的基本使用
- Android中的Service的使用详解(一)
- Service的学习(一)
- Service的生命周期(一)
- Service详解(三):Service的使用
- Android Service 的使用(二)----Service的绑定使用
- Android Service 的使用(一)----简单Serivce的启动和停止
- Random类和Math.radnom()方法的联系
- POJ 2431 Expedition(优先队列)
- HDU 3743 Frosh Week 【归并排序模板题】
- 20150812-Mapreduce笔记
- jquery Deferred 数组
- Service的使用(一)
- 在.gitignore中过滤不必要的文件
- 又见回文
- hdu5373
- 在CodeWarrior中指定堆栈大小及堆栈指针
- SQLite集成与用法
- gflags调试工具的使用
- 函数
- Database Initialization Strategies in Code-First & Seed Data(sample role)