为什要使用BindService?为了调用服务中的方法
来源:互联网 发布:linux怎么读音是什么 编辑:程序博客网 时间:2024/06/05 21:54
TestService类:package com.itheima74.whybindservice.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;/** * 0.定义一个服务,需要在清单文件注册 * Created by My on 2017/2/23. */public class TestService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); // 3.将中间人对象返回 return new MyBinder(); } @Override public void onCreate() { System.out.println("onCreate"); super.onCreate(); } @Override public void onDestroy() { System.out.println("onDestroy"); super.onDestroy(); } /** * 1.定义服务中的TestMethod方法 */ public void testMethod() { System.out.println("我是服务中的TestMethod方法"); } /** * 2.定义中间人对象(IBinder类的实例),它能间接调用服务中的方法 */ public class MyBinder extends Binder { public void callTestMethod() { TestService.this.testMethod(); } }}MainActivity类:
package com.itheima74.whybindservice;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.itheima74.whybindservice.service.TestService;/** * 为什要BindService? * 为了调用服务中的方法 */public class MainActivity extends AppCompatActivity { private MyConn mMConn; private TestService.MyBinder mBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 4.连接到服务 Intent intent = new Intent(this, TestService.class); mMConn = new MyConn(); bindService(intent, mMConn, BIND_AUTO_CREATE); } // 6.点击按钮,调用服务中的方法 public void click(View view) { mBinder.callTestMethod(); } private class MyConn implements ServiceConnection { // 服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { // 5.获取中间人对象 mBinder = (TestService.MyBinder) service; } // 服务失去连接调用 @Override public void onServiceDisconnected(ComponentName name) { } } @Override protected void onDestroy() { super.onDestroy(); // 7.Activity销毁时取消绑定服务,否则会报红色日志; unbindService(mMConn); }}
运行结果:
02-23 07:38:56.337 2522-2522/com.itheima74.whybindservice I/System.out: onCreate02-23 07:38:56.338 2522-2522/com.itheima74.whybindservice I/System.out: onBind02-23 07:38:59.358 2522-2522/com.itheima74.whybindservice I/System.out: 我是服务中的TestMethod方法02-23 07:39:00.531 2522-2522/com.itheima74.whybindservice I/System.out: 我是服务中的TestMethod方法02-23 07:39:01.148 2522-2522/com.itheima74.whybindservice I/System.out: 我是服务中的TestMethod方法
0 0
- 为什要使用BindService?为了调用服务中的方法
- android bindService 来调用服务中的方法
- 通过bindservice调用服务内部方法
- 通过bindService调用服务里面的方法
- android服务_通过bindService调用服务里的方法
- Android通过bindservice方式调用服务的方法
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- 服务的创建,绑定以及调用服务中的方法(以应用锁为例)
- 绑定服务:调用远程服务中的方法
- 绑定服务并调用服务中的方法
- 绑定服务调用本地服务中的方法
- 绑定服务调用远程服务中的方法
- bindService调用Service里面方法的步骤
- Android之使用bindService启动服务
- 服务启动方式三-使用bindService方式启动服务
- 客户端动态调用WCF服务中的方法
- Android 利用广播调用服务中的方法
- Android Service 调用服务中的方法
- PHP-简单分页和文件上传类
- 简单的shell程序
- int * const *, const int * const *, const int **
- 【bzoj3238】差异 后缀自动机
- Python基础教程(2)
- 为什要使用BindService?为了调用服务中的方法
- codeforces 528D. Fuzzy Search (FFT优化DP)
- 453. Minimum Moves to Equal Array Elements
- leetcode总结帖
- c++ 调试问题 集合
- 55. Jump Game
- 【BZOJ 2618】[Cqoi2006]凸多边形 半平面交
- 来电去电自动录音
- 上下移动页面元素的代码