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. C#的math类的全部运算方法

    Abs 返回指定数字的绝对值.Acos 返回余弦值为指定数字的角度.Asin 返回正弦值为指定数字的角度.Atan 返回正切值为指定数字的角度.Atan2 返回正切值为两个指定数字的商的角度.BigM ...

  2. 移动端 - APP测试要点

    功能测试 1.运行 1)App安装完成后的试运行,可正常打开软件. 2)App打开测试,是否有加载状态进度提示. 3)App页面间的切换是否流畅,逻辑是否正确. 2.注册 1)同表单编辑页面 2)用户 ...

  3. dropzone 上传插件

    dropzone dropzone.js是一个可预览的上传文件工具,不依赖任何框架(如jQuery),且具有可定制化.实现文件拖拽上传,提供AJAX异步上传功能. 1. html文件 dropzone ...

  4. 最长上升子序列 and 最长公共子序列 问题模板

    两种求最长上升子序列问题 第一种:定义dp[i]=以a[i]为末尾的最长上升子序列问题的长度 第二种:定义dp[i]=长度为i+1的上升 子序列 中末尾元素的最小值 #include <cstd ...

  5. 9.DataGrid数据表格

    后台获取数据并将其转换为json数组格式: 前台获取数据并显示在数据表格中:

  6. 2018/6/29 关于hashmap的总结

    hashMap和ConcurrentHashMap的区别 hashMap内部具体如何实现的 如果hashMap的key是一个自定义的类,怎么办 为什么重写equals还要重写hashcode 一.什么 ...

  7. SRM481

    250pt 题意:上帝知道一个“先有鸡还是先有蛋”的答案,上帝和N<=10^6个人说了答案,不过有x个人故意告诉了他们错误的答案,然后有一个人问了这N个人问题的答案,有M个人说先有鸡,N-M个人 ...

  8. 雪花算法(snowflake)delphi版

    雪花算法简单描述: + 最高位是符号位,始终为0,不可用. + 41位的时间序列,精确到毫秒级,41位的长度可以使用69年.时间位还有一个很重要的作用是可以根据时间进行排序. + 10位的机器标识,1 ...

  9. Android-HttpClient-Get与Post请求登录功能

    HttpClient 是org.apache.http.* 包中的: 第一种方式使用httpclient-*.jar (需要在网上去下载httpclient-*.jar包) 把httpclient-4 ...

  10. kafka各个版本特点介绍和总结

    kafka各个版本特点介绍和总结 1.1 kafka的功能特点: 分布式消息队列 消息队列的数据模型, 形成流式数据. 提供Pub/Sub方式的海量消息处理.以高容错的方式存储海量数据流.保证数据流的 ...