Android Service 服务的基本用法
2016-07-24 13:36
916 查看
Service 服务的基本用法
创建&绑定服务活动与服务进行通信
创建&绑定服务
活动与服务进行通信
创建四个按钮:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.scott.servicetest.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/start_service" android:text="Start Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/stop_service" android:text="Stop Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bind_service" android:text="Bind Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/unbind_service" android:text="Unbind Service" android:textAllCaps="false"/> </LinearLayout>
package com.example.scott.servicetest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button startService; private Button stopService; private Button bindService; private Button unbindService; private Intent intent;//不能这么写Intent intent = new Intent(MainActivity.this,MyService.class); private MyService.DownloadBinder downloadBinder; //创建一个SerciceConnection的匿名类并重写下面的两个方法,这两个方法分别会在活动与服务成功绑定和解除绑定的时候调用 private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //向下转型获取DownloadBinder实例 downloadBinder = (MyService.DownloadBinder) service; downloadBinder.startDownload(); downloadBinder.getProgress(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //~~~~~ intent = new Intent(MainActivity.this,MyService.class); startService = (Button) findViewById(R.id.start_service); stopService = (Button) findViewById(R.id.stop_service); bindService = (Button) findViewById(R.id.bind_service); unbindService = (Button) findViewById(R.id.unbind_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.start_service: startService(intent); break; case R.id.stop_service: stopService(intent); break; case R.id.bind_service: //这里传入的BIND_AUTO_CREATE表示在活动和服务进行绑定的时候会自动执行onCreate(),不会执行onStartCommand() bindService(intent,connection,BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; default: break; } } }
总结一下,onCreate()方法是在服务第一次创建的时候调用,onStartCommand()方法是在每次启动服务的时候调用。
活动与服务进行通信:在MyService中创建一个继承自Binder的实例;然后在onBind()中返回这个实例。然后再活动中绑定服务,就可以服务里的Binder提供的方法了。
关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。
相关文章推荐
- Android最佳性能实践(三)——高性能编码优化
- Android最佳性能实践(二)——分析内存的使用情况
- API使用的Android权限列表(Permissions)
- Android最佳性能实践(一)——合理管理内存
- 高效的显示bitmap
- Android系统源码阅读(1):编译
- 谈谈Android 6.0 的动态权限管理
- AudioManager 的使用
- 做Android开发 需要掌握哪些知识
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- Android开发代号和对应API等级
- 【Android】子Fragment获取父Fragment
- Android应用程序获取system权限
- Android 4.4系统下LayoutParam使用
- android MVVM开发模式(二)
- 【Android】 获取当前系统语言
- Android获取屏幕尺寸的方法
- 新建android项目时,ActionBarActivity找不到
- bitmap 多级缓存
- Android使用Messenger实现进程间双向通信