1.创建服务

Exported:是否允许除了当前程序之外的其他程序访问这个服务

Enable:是否启用这个服务

点击完成后自动生成

import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}

onBind 是Service唯一的抽象方法。

创建完成后会在AndroidManifest.xml中自动进行注册

 <service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>

2.开关服务

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void startClick(View view){
Intent startIntent=new Intent(this,MyService.class);
//启动服务
startService(startIntent);
}
public void stopClick(View view){
Intent startIntent=new Intent(this,MyService.class);
//关闭服务
stopService(startIntent);
}
}

界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:onClick="startClick" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="stopClick" />
</LinearLayout>

点击启动服务按钮,在手机系统里可以发现多出一个服务名,

即使关闭程序,服务也不会停止。直到点击停止服务按钮,或者卸载了该程序。

3.

public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
//服务创建时调用
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService","执行了onCreate方法");
}
//服务每次启动时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService","执行了onStartCommand方法");
return super.onStartCommand(intent, flags, startId);
}
//服务销毁时调用
@Override
public void onDestroy() {
Log.d("MyService","执行了onDestroy方法");
super.onDestroy();
}
}

第一次点击启动服务

再次点击启动服务

点击停止服务

4.关联活动与服务

添加两个按钮

  <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="bind" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消绑定"
android:onClick="unBind" />

MyService

public class MyService extends Service {

    private Download dlBinder =new Download();

    class Download extends Binder{

        public void startDownload(){
Log.d("MyService","开始下载");
}
public int getProgress(){
Log.d("MyService","得到进度");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return dlBinder; }

MainActivity

public class MainActivity extends AppCompatActivity {
private MyService.Download dlBinder;
//匿名类
private ServiceConnection connection =new ServiceConnection() {
//活动与服务成功绑定时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//
dlBinder=(MyService.Download)service;
dlBinder.startDownload();
dlBinder.getProgress();
}
//活动与服务解除绑定时调用
@Override
public void onServiceDisconnected(ComponentName name) {
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } public void bind(View view){
Intent bindIntent=new Intent(this,MyService.class);
//绑定服务
bindService(bindIntent,connection,BIND_AUTO_CREATE);
} public void unBind(View view){
Intent unBindIntent=new Intent(this,MyService.class);
//解绑服务
unbindService(connection);
}
}

点击 绑定服务

点击取消绑定

最新文章

  1. WIN10 多用户登录
  2. Java项目多数据源配置
  3. 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(五)
  4. c++ encode decode
  5. Discuz! x 2.5-3.0 beta 存储型跨站漏洞
  6. MySQL DBA修炼秘籍
  7. Spring注入静态变量(转)
  8. PHP从数据库获取的下拉树
  9. UICollectController
  10. Yii框架基础增删查改
  11. pam密码策略
  12. Android优化指南
  13. struts2中ognl标签具体解释
  14. Python Pycharm连接Ubantu Python环境
  15. align-items和align-content的区别
  16. 原生的 promise 的局限性
  17. Promise面试题
  18. 长城防火墙(GFW)
  19. Android 跑马灯效果与EditText冲突
  20. vm装xp安装成功后进入不了系统

热门文章

  1. python之dic {字典}(重要指数*****)
  2. fclose - 关闭流
  3. oracle中group by的高级用法
  4. qemu-img管理虚拟机
  5. vue axios 请求本地接口端口不一致出现跨域设置代理
  6. 【数论】贝壳找房计数比赛&amp;&amp;祭facinv
  7. RESTful介绍
  8. 设置 TabBarItemt图片颜色
  9. LeetCode 637. Average of Levels in Binary Tree(层序遍历)
  10. Hive 执行sql命令报错