关于Service的学习与使用
2018-03-21 16:31
453 查看
一、Service的含义
Service是Android的四大组件之一,级别与Activity相当Service长时间运行在后台,是不可见、无界面的组件
在主线程运行
可跨进程调用
启动方式:startService和bindService
二、以startService启动Service使用步骤
新建一个类继承Service重写onCreate方法
实现onBind方法抽象方法(无需理会)
重写onStartCommand方法
重写onDestory方法
在AndroidManifest中注册Service
在有Context里通过startService和stoptService启动和关闭Service
三、以bindService启动Service使用步骤
新建类继承Service实现onBind抽象方法
注意点:在此方法中需要返回一个Binder子类对象
重写onCreate方法
重写onUnbind方法
重写onDestory方法
在AndroidManifest中注册Service
在有Context环境中实现ServiceConnection接口
注意点:ServiceConnection带来两个两个方法,分别是onServiceConnected和onServiceDisconnected方法。即是否连接成功,后种一般无需理会
在有Context环境中通过bindService绑定Service
在有Context环境中unbindService绑定Service
注意点:纯属个人理解,我认为的“在有Context环境中”就是你所创建额主体类里
四、 startService和bindService分别启动Service的优缺点
1.startService启动Service的优点:使用简单,只需几行代码就可启动Service缺点:无法获得Service对象,所以不能直接操作Service中的属性和方法
2.bindService启动Service的优点:可以得到Service的对象,灵活控制Service内部的属性和方法
缺点:使用较复杂
五、用startService启动Service的实例(以计时为例)
1.首先在layout布局文件里创建出必要的控件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.service.MainActivity"> <Button android:id="@+id/start_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始"/> <Button android:id="@+id/stop_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止"/> </LinearLayout>
2.回到主体类里定义控件,绑定ID,设置监听事件
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button startBtn; private Button stopBtn;
bindId(); }
private void bindId() { startBtn=findViewById(R.id.start_btn); stopBtn=findViewById(R.id.stop_btn); startBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); } @Override public void onClick(View view) {
3.在AndroidManifest中注册Service
<service android:name=".MyService"></service>
4.新建一个类继承Service,并实现她的几个方法,并把线程名打印出来(与第五步有关)
public class MyService extends Service { public void onCreate(){ super.onCreate(); Log.d("MyService"+Thread.currentThread().getName(),"onCreate executed"); }
public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService","onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
public void onDestroy() { super.onDestroy(); Log.d("MyService"+Thread.currentThread().getName(),"onDestroy executed"); }
5.在onStartCommand方法里建一个子线程,实现计时效果
new Thread(new Runnable() { @Override public void run() { for(int i=0;i<10;i++){ Log.e("MyService"+Thread.currentThread().getName(),i+"***********"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();
6.在主体类里的点击方法写switch判断给两个按钮设置启动和关闭
switch (view.getId()){ case R.id.start_btn: Intent startIntent = new Intent(MainActivity.this, MyService.class); // 绑定服务,启动 startService(startIntent); break; case R.id.stop_btn: Intent stopIntent = new Intent(MainActivity.this, MyService.class); // 绑定服务,关闭 stopService(stopIntent); break; }
六、用bindService启动Service的实例(以打印开飞机为例)
1.首先在layout布局文件里新建一些需要的控件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.service.Main2Activity"> <Button android:id="@+id/start_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="绑定服务"/> <Button android:id="@+id/stop_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="解绑服务"/> </LinearLayout>
2.回到主体类里定义,绑定这些控件并设置监听事件
public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private Button startBtn; private Button stopBtn;
private void bindId() { startBtn = findViewById(R.id.start_btn); stopBtn = findViewById(R.id.stop_btn); startBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.start_btn: break; case R.id.stop_btn: break;
tips:点击方法里用了switch判断语句,来判断点击了哪个按钮
3.新建一个类来继承Service,然后实现他的几个方法
public class MyBindService extends Service {<
bfcd
/pre>
public IBinder onBind(Intent intent) {} @Override public void onCreate() { super.onCreate(); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); } }
4.然后自定义一个类来继承Binder,目的是得到Service对象public class Guangjia extends Binder { //作用:得到当前Service对象 public MyBindService getServiceObject() { return MyBindService.this; } }
5.在全局把对象new一下private Guangjia guangjia=new Guangjia();
6.接写一个方法类public void fly(){ Log.e(TAG,"开飞机..."); }MyBindService myBindService=((MyBindService.Guangjia)iBinder).getServiceObject(); myBindService.fly();
通过管家的中介作用,拿到MyBindService对象
7.紧接着在主体类里的点击方法里实现绑定效果case R.id.start_btn: Intent intent=new Intent(this,MyBindService.class); bindService(intent,serviceConnection,BIND_AUTO_CREATE); break;
8.最后一定要注意解除绑定case R.id.stop_btn: unbindService(serviceConnection); break;七、IntentService的定义
IntentService继承于Service并处理异步任务的一个类
内部有一个工作线程来处理耗时操作
线程无需我们控制和维护
当多次启动时,会以队列的形式,逐一执行
当执行完耗时工作时,会自动停止八、IntentService的使用步骤
新建类继承IntentService
实现父类的构造方法
重写onHandlelntent方法
注意点:这是一个子线程,耗时工作在这里执行
实现onCreate方法
重写onDestory方法
在AndroidManifest中注册Service
在有Context环境中通过StartService启动Service九、用IntentService启动Service的实例(以倒计时为例)
1.首先在layout布局文件里定义控件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.service.MyIntentActivity"> <Button android:id="@+id/start_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始"/> </LinearLayout>
2.在主体类里定义控件,绑定ID,设置监听public class MyIntentActivity extends AppCompatActivity { private Button startBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_intent); startBtn = findViewById(R.id.start_btn); startBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
3.新建一个类继承IntentService,来实现他的几个方法public class MyIntentService extends IntentService{ public MyIntentService(String name) { super(name); } public MyIntentService(){ super(""); } @Override //此方法是在子线程进行,直接在里面进行耗时操作 protected void onHandleIntent(@Nullable Intent intent) {public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } }
4.然后在他的onHandleIntent方法里写一个for循环,来实现倒计时效果for(int i=0;i<10;i++){ try { Log.e("MyIntentService",i+"******"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }
5.最后在AndroidManifest中注册Service<service android:name=".MyIntentService" />
笔芯!
相关文章推荐
- 关于批处理中FOR语句的使用说明与学习技巧
- 关于使用ssh框架时,pojo、dao、service三层的关系
- Shell基础学习:关于Wget命令的使用技巧[zt]
- Reporting Service Tips 101(#2) - 关于使用Sum函数会遇到的问题(1)
- C++之父元旦专访(8+13个问题,关于C++的学习&使用和未来)
- 关于js、css使用的学习
- 关于PHP使用开源图表控件的学习
- 关于MSSQL存储过程中使用游标的一个小例子(学习)
- 2009-05-24技术学习:Java中使用Axis调用.Net WebService
- 关于JQuery 插件Grid的学习使用笔记 (PHP版)
- Reporting Service Tips 101(#2) - 关于使用Sum函数会遇到的问题(1)
- ASP.NET学习笔记[2] - 关于使用Master.Page的几点注意事项
- 关于事件驱动学习三 使用select的socket callback模型
- 关于网络设备的学习使用
- 学习使用资源文件[8] - 关于 HInstance
- C++之父元旦专访(8+13个问题,关于C++的学习&使用和未来)
- 关于RDLC报表的一些总结,希望对正在学习使用它的朋友有所帮助
- C++之父元旦专访(8+13个问题,关于C++的学习&使用和未来)
- 关于MSChart的简单学习与使用
- 关于STL容器输出的更进一步简化,便于平时学习使用