前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android使用bindService作为中间人对象开启服务

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

作者头像
Dream城堡
发布2019-01-28 10:52:21
5730
发布2019-01-28 10:52:21
举报
文章被收录于专栏:Spring相关Spring相关

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

项目结构如下:

image

MyService:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n14" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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); ​ } ​ ​ } } ​</pre>

MainActivity:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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(); } } ​</pre>

activity_main.xml:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n36" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?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></pre>

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android使用bindService作为中间人对象开启服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档