Android使用bindService作为中间人对象开启服务

项目结构如下:



MyService:

package com.demo.secondservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast; public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) { return new MyBind();
} public void banZheng(int money) { if(money>1000){
Toast.makeText(getApplicationContext(), "帮你办", Toast.LENGTH_LONG).show(); }else { Toast.makeText(getApplicationContext(), "钱少,不办", Toast.LENGTH_LONG).show(); }
} //定义中间人
public class MyBind extends Binder{ public void callBanZheng(int money){ //调用办证的方法
banZheng(money); } }
}

MainActivity:

package com.demo.secondservice;

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; public class MainActivity extends AppCompatActivity {
MyConn myConn;
MyService.MyBind myBind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
// startService(intent);
//连接服务
myConn = new MyConn();
bindService(intent,myConn,BIND_AUTO_CREATE);
} public void click(View view) { //自己new对象 脱离了谷歌框架
// MyService myService = new MyService();
myBind.callBanZheng(10200); } //监视服务的状态
private class MyConn implements ServiceConnection{ //当服务连接成功调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) { myBind = (MyService.MyBind) service; } //失去连接
@Override
public void onServiceDisconnected(ComponentName name) { }
} @Override
protected void onDestroy() { //解绑服务
unbindService(myConn);
super.onDestroy();
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启服务"
/> </android.support.constraint.ConstraintLayout>

Android使用bindService作为中间人对象开启服务的更多相关文章

  1. [android] 绑定方式开启服务&调用服务的方法

    需求:后台开启一个唱歌服务,这个服务里面有个方法切换歌曲 新建一个SingService继承系统Service 重写onCreate()和onDestory()方法 填一个自定义的方法changeSi ...

  2. Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务

    1. 概述 bindService() 绑定服务  可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法.           如果调用者activity被销毁了, ...

  3. Android服务之混合方式开启服务

    引言 前面介绍过了Android服务的两种开启方式:Start方式可以让服务在后台运行:bind方式能够调用到服务中的方法. 在实际的开发工作中,有很多需求是:既要在后台能够长期运行,又要在服务中操作 ...

  4. Android service ( 一 ) 三种开启服务方法

    一. Service简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟 Activ ...

  5. Android(java)学习笔记231:服务(service)之混合方式开启服务

    1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService:        oncreate() ---> onstartCommand() ---& ...

  6. Android(java)学习笔记174:服务(service)之混合方式开启服务

    1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService:        oncreate() ---> onstartCommand() ---& ...

  7. start方式开启服务的特点&bindService 方式开启服务的特点

      服务是在后台运行 可以理解成是没有界面的activity   定义四大组件的方式都是一样的     定义一个类继承Service     start方式开启服务的特点   特点:   (1)服务通 ...

  8. Android 编程下的四大组件之服务(Service)

    服务(Service) 是一种在后台运行,没有界面的组件,由其他组件调用开始.Android 中的服务和 Windows 中的服务是类似的东西,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类 ...

  9. Android学习笔记(五一):服务Service(上)- IntentService

    转自 http://blog.csdn.net/flowingflying/article/details/7616333 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的ac ...

随机推荐

  1. textInput事件

    DOM3级事件引入了 textInput 这个代替keypress的textInput的行为稍有不同 区别 只要可以获得焦点的元素都有keypress事件,但是textInput事件只有文本编辑区域才 ...

  2. git,版本控制教程

    主要内容 版本回退 工作区和暂存区 管理修改 撤销修改 删除文件 分支管理     *****此处没有深讲***** 一.两条基本查看命名 查看状态命令: git status 查看修改内容命令: g ...

  3. 20169207《Linux内核原理与分析》第三周作业

    这周主要对于以往的知识进行了复习,包括开始的Linux基础,以及对需要学习的新知识进行了复习,也对markdown的语法进行了初步的了解.开始学习markdown的一些基本语法和Linux的一些常用命 ...

  4. linux 各项分布(个人记录)

    1.根目录文件 root:存放root用户的相关文件home:存放普通用户的相关文件bin :存放常用命令的目录sbin:要具有一定权限才可以使用的命令mnt :挂在光驱和软盘的目录boot:存放引导 ...

  5. java基础-day27

    第04天 java基础加强 今日内容介绍 u Xml的综合案例 u 注解 u 类的加载 u 动态代理 第1章   注解 1.1  注解概述 l  什么是注解:Annotation注解,是一种代码级别的 ...

  6. 第73讲:Scala界面和事件处理编程进阶实战

    今天学习了王家林老师讲解的scala编程的第73讲,主要是文件选择器的使用.让我们通过代码来亲身体验一下. import scala.swing._import java.io.Fileimport ...

  7. ASP.NET实现头像剪切保存

    利用swfupload上传头像,利用Jcrop来实现头像在线选择,然后提交个ashx对原头像进行剪切.代码如下: default.aspx: <%@ Page Language="C# ...

  8. django天天生鲜项目

    .后台admin管理天天生鲜商品信息 models里 from django.db import modelsfrom tinymce.models import HTMLField #需要pip安装 ...

  9. hive 函数 current_date()

    current_date() 获取当天时间 select current_date() 结果是  实际操作中,这种写法只能获取日期是今天的数据,不适合数据重跑. 更合适的写法是 ,将日期作为外部参数传 ...

  10. 转 iOS宏定义的使用与规范

    宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中,下面我抛砖引玉,对 ...