android Service基础(启动服务与绑定服务)
作者:不如沉默啊 发布时间:2023-05-07 12:31:34
标签:android,Service
Service是Android中一个类,它是Android 四大组件之一,使用Service可以在后台执行耗时的操作(注意需另启子线程),其中Service并不与用户产生UI交互。其他的应用组件可以启动Service,即便用户切换了其他应用,启动的Service仍可在后台运行。一个组件可以与Service绑定并与之交互,甚至是跨进程通信。通常情况下Service可以在后台执行网络请求、播放音乐、执行文件读写操作或者与contentprovider交互等。
本文主要讲述service服务里的启动与绑定服务。
首先,XML程序如下:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="operate"
android:text="启动服务" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="operate" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="operate" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑服务"
android:onClick="operate"/>
</LinearLayout>
创建一个service类,并写创建、启动、绑定、摧毁、解绑五个方法
代码如下:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private int i;
public MyService() {
}
//创建
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG","服务创建了");
}
class MyBinder extends Binder {
//定义自己需要的方法(实现进度监控)
public int getProcess(){
return i;
}
}
//启动方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","服务启动了");
return super.onStartCommand(intent, flags, startId);
}
//解绑
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG","服务解绑了");
return super.onUnbind(intent);
}
//摧毁
@Override
public void onDestroy() {
Log.e("TAG","服务摧毁了");
super.onDestroy();
}
//绑定方法
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
Log.e("TAG","服务绑定了");
return new MyBinder();
}
}
然后在MainActivity里面写点击启动方法:
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//启动服务 :创建——启动——摧毁
//如果服务已经创建了,后续重复启动,操作的都是同一个服务,不回在创建新 的服务,除非先摧毁他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
}
}
然后写绑定的方法:
注意:捆绑和解绑的对象应该是同一个对象,如果捆绑与解绑的对象不一样,则会报如下的错误:
Service not registered: com.m1910.servicetest.MainActivity$1@41ddfcc0
本篇文章捆绑与解绑的对象都是conn,定义一个全局变量:
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
然后写解绑与捆绑的方法:
case R.id.bind:
//绑定服务
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解绑服务
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解绑服务
// isBound = false;
// }
unbindService(conn);//解绑服务
break;
完整的程序如下:
package com.example.service;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private boolean isBound = false;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//启动服务 :创建——启动——摧毁
//如果服务已经创建了,后续重复启动,操作的都是同一个服务,不回在创建新 的服务,除非先摧毁他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
case R.id.bind:
//绑定服务
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解绑服务
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解绑服务
// isBound = false;
// }
unbindService(conn);//解绑服务
break;
}
}
}
来源:https://blog.csdn.net/weixin_58308529/article/details/122185689


猜你喜欢
- 本文实例讲述了java设计模式之工厂模式。分享给大家供大家参考,具体如下:工厂模式(factory)涉及到4个角色:抽象工厂类角色,具体工厂
- JVM内部结构图Java虚拟机主要分为五个区域:方法区、堆、Java栈、PC寄存器、本地方法栈。下面来看一些关于JVM结构的重要问题。1.哪
- 消息的可靠投递在使用 RabbitMQ 的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败场景。RabbitMQ 为我们提供了两种方式用
- 1.定义指向非法的内存地址指针叫作野指针(Wild Pointer),也叫悬挂指针(Dangling Pointer),意为无法正常使用的指
- 一丶前言在之前我们学习了SpringBoot自动装配如何实现的,我们总结了Spring IOC的底层原理。但是我们还是不知道SpringAp
- 前言最近在参与一个银行项目-某银行安防系统-反洗钱需求的开发,银行项目的离不开身份证号码,身份证号码作为我国公民的唯一标识,有这非同寻常的意
- 参考: https://gist.github.com/laispace/666dd7b27e9116faece6前提是你本地有 socks
- 参考:How to catch an Exception from a threadIs there a way to make Runna
- 一、实战-内存溢出堆内存溢出栈内存溢出方法区溢出直接内存溢出二、实战-堆内存溢出演示堆内存溢出代码,并且定位问题总结堆内存溢出的场景与解决方
- 一.工程文件二.Main.java主函数,实现类package ui;//主函数实现public class Main { &
- 在android开发中,当不满足触发条件就按返回键的时候,就要对此进行检测。尤其是当前Activity需要往前一个Activity传送消息时
- Synchronized的用法在多线程并发问题中,常用Synchronized锁解决问题。Synchronized锁通常用于同步示例方法,同
- 将jar包发布到Maven中央仓库(Maven Central Repository),这样所有的Java开发者都可以使用Maven直接导入
- 本文实例讲述了java实现给出分数数组得到对应名次数组的方法。分享给大家供大家参考。具体实现方法如下:package test01;/**
- import java.util.List;/*** * 基本接口 * * @author xyq 
- 1.为项目添加POIPOI官网链接点进去之后下载(上边的是编译好的类,下边的是源代码) 解压文件夹,把下面三个文件复制到WebCo
- 1.插入排序这个打麻将或者打扑克的很好理解, 比如有左手有一副牌1,2,4,7 ,来一张3的牌, 是不是就是手拿着这张牌从右往左插到2,4之
- 引言在项目中,时间的使用必不可少,而java 8之前的时间api Date和Calander等在使用上存在着很多问题,于是,jdk1.8引进
- 描述:由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assis
- 环境配置:jdk1.8mybatis3.4.1springboot2.0起始原因:编写mybatis的Demo程序时,mapper传递多参数