美文网首页我爱编程
服务,调用方法

服务,调用方法

作者: 三度_f8ac | 来源:发表于2018-05-25 15:17 被阅读0次

import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity {private MyConnection conn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overrideprotected void onDestroy() {super.onDestroy();//把Service关闭,解除跟当前Activity的绑定unbindService(conn);}public void start(View v){Intent service = new Intent(this,BindService.class);//通过bind方法实现service方法 conn = new MyConnection(); //使用bindService开启的服务//参数1:包含要启动个Service //参数2:ServiceConnection接口,通过它可以接受到服务开启或者结束的消息 //参数3:开启服务是操作的选项,一般传入BIND_AUTO_CREATE,自动创建ServicebindService(service, conn, BIND_AUTO_CREATE);}public void stop(View v){//使用bindService开启的服务要用unbindService来停止unbindService(conn);}public void callServiceMethod(View v){BindService service = new BindService();service.showToast("hello");}private class MyConnection implements  ServiceConnection{private MyBinder myBinder;@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//只有当Service的onBind方法返回值不为null,才会调用此方法Log.e("TAG", "onServiceConnected"); myBinder = (MyBinder) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {//服务正常退出的时候不会调用此方法Log.e("TAG", "onServiceDisconnected");}}}import android.app.Service;import android.content.Intent;import android.os.Binder;importandroid.os.IBinder;import android.util.Log;

import android.widget.Toast;public class BindService extends Service{@Overridepublic IBinder onBind(Intent intent) {Log.e("TAG", "onBind");return new  MyBinder();}@Overridepublic void onCreate() {Log.e("TAG", "onCreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.e("TAG", "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.e("TAG", "onDestroy");super.onDestroy();}public void showToast(String s){Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT);}public class MyBinderextends Binder{public void callShowToast(String s){showToast(s);}public void showToast2(String s){Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT);}}}

相关文章

  • 服务,调用方法

    import android.app.Activity;import android.content.Compon...

  • RPC 客户端与服务端工作原理

    对于服务调用者来说: (1)服务框架获得服务调用者提供的服务信息(服务唯一标识:接口全限定名+版本号;方法;调用参...

  • [knowledgePoint]_[Service]

    最常用的三个方法 Summary onCreate 创建服务时调用 onStartCommand 启动服务时,调用...

  • 服务引用RestTemplate

    前言 本文介绍如何调用服务,调用服务有如下方法: RestTemplate,直接使用RestTemplate,写死...

  • Service

    Service生命周期 onCreate() 首次创建服务时,系统将调用此方法。如果服务已在运行,则不会调用此方法...

  • Android 学习笔记

    对一个服务既调用了startService()方法,又调用了bindService()方法的,这种情况下要同时调用...

  • 调用服务中的方法

    服务中的方法不能通过直接 new对象的方法调用,必须通过ibinder作为中间人调用 1、定义服务类 2、定义测试...

  • HttpClient 详解一《C#高级编程(第9版)》

    1.异步调用 Web 服务 来看看下面方法解释: 因为 HttpClient 使用 GetAsync 方法调用,且...

  • Service

    方法介绍: onCreate():服务第一次被创建时调用 onStartComand():服务启动时调用 onBi...

  • Create TradeGrpc

    1.定制服务接口 2.创建客户端,调用具体方法 3.创建服务端 在服务端写好调用方法 4.在服务端startup....

网友评论

    本文标题:服务,调用方法

    本文链接:https://www.haomeiwen.com/subject/gpqujftx.html