1.绑定service

2.实现方法

3.在Androidmanifest.xml文件中配置service

<service android:name=".Myservice"></service>

4.java后台

MainActivity界面

package com.lucky.test38service2;

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;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
Button button_setbind;
Button button_unsetbind;
Button button_getvalue;
Button button_clear;
Intent intent;
Myservice.Mybind mybind; //定义一个Mybind
ServiceConnection connection; //定义一个ServiceConnection @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_setbind=findViewById(R.id.button_setbind);
button_unsetbind=findViewById(R.id.button_unsetbind);
button_getvalue=findViewById(R.id.button_getvalue);
button_clear=findViewById(R.id.button_clear);
intent=new Intent(MainActivity.this,Myservice.class); //ServiceConnection 建立service连接
connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mybind= (Myservice.Mybind) service; //利用mybind作为MainActivity与Myservice信息交互的桥梁
} @Override
public void onServiceDisconnected(ComponentName name) { }
}; button_setbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent,connection,BIND_AUTO_CREATE); //绑定service
}
});
button_unsetbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection); //解除service
}
});
button_getvalue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示service内容
Toast.makeText(MainActivity.this,mybind.getCount()+"",Toast.LENGTH_SHORT).show();
}
});
button_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mybind.setCount(0); //设置count值
}
});
}
}

Myservice

package com.lucky.test38service2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class Myservice extends Service { boolean flag1;
int count; //新建绑定工具类,绑定count
public class Mybind extends Binder{
//获取count值
public int getCount(){
return count;
} //设置count值
public void setCount(int arg){
count=arg;
}
} Mybind mybind=new Mybind(); @Override
public IBinder onBind(Intent intent) {
return mybind;
} //service创建时调用的方法
@Override
public void onCreate() {
super.onCreate();
flag1=true;
new Thread(){
@Override
public void run() {
while (flag1){
try {
Thread.sleep(1000); //延时线程
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
super.onDestroy();
flag1=false;
} }

Android 绑定Service并与之通信的更多相关文章

  1. android绑定Service失败原因

    今天抄一个代码,学习Service,中间Service的绑定一直是失败的. bindService返回false 上网查询的话都是一些,比如说TabHost的问题 发现和自己的问题不一样. 最后想了想 ...

  2. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  3. android学习-IPC机制之ACtivity绑定Service通信

    bindService获得Service的binder对象对服务进行操作 Binder通信过程类似于TCP/IP服务连接过程binder四大架构Server(服务器),Client(客户端),Serv ...

  4. Android菜鸟的成长笔记(18)——绑定本地Service并与之通信

    在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...

  5. Android中AIDL的理解与使用(一)——跨应用启动/绑定Service

    AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...

  6. Android开发学习之路-Service和Activity的通信

    在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...

  7. Android Service与Activity之间通信的几种方式

    在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...

  8. 绑定本地Service并与之通信

    绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...

  9. (六)Android中Service通信

    一.启动Service并传递参数 传递参数时只需在startService启动的Intent中传入数据便可,接收参数时可在onStartCommand函数中通过读取第一个参数Intent的内容来实现 ...

随机推荐

  1. Django开发——集成的子框架django.contrib

    Django开发——集成的子框架django.contrib 2018年09月11日 19:32:42 Mrkang1314 阅读数:63  https://blog.csdn.net/mashaok ...

  2. Python中常用模块二

    一.hashlib   (加密) hashlib:提供摘要算法的模块 1.正常的md5算法 import hashlib # 提供摘要算法的模块 md5 = hashlib.md5() md5.upd ...

  3. java - Logback获取方法名称

    java - Logback获取方法名称 摘自: https://blog.csdn.net/qq853632587/article/details/78222780 我们目前正在从 Log4J 迁移 ...

  4. ESP8266文档阅读2A-SDK-Espressif IoT SDK 使用手册v1.0.1.pdf

    2A-SDK-Espressif IoT SDK 使用手册v1.0.1.pdf 1.前言 本⽂文主要介绍基于ESP8266物联⺴⽹网模块的SDK相关使⽤用⽅方法,包括开发⼯工具使⽤用以及SDK软件包架 ...

  5. jQuery提供的存储接口

    jQuery.data( element, key, value ) //静态接口,存数据jQuery.data( element, key ) //静态接口,取数据 .data( key, valu ...

  6. PHP文件的引用

    require "文件名" 或 include("文件名") 区别:若所包含文件出现错误,include()产生一个警告,require会导致程序终止

  7. Java 之集合框架

  8. python3 tkinter 获取输入字符串长度

    python 3  获取输入字符长度 #-*- coding:utf-8 -*- from tkinter import * from tkinter import messagebox def ge ...

  9. Android Tablayout属性介绍

    1.添加依赖 compile 'com.android.support:design:26.0.0-alpha1' 2.属性 改变选中字体的颜色app:tabSelectedTextColor=&qu ...

  10. 使用Oracle(SQL Plus)

    error: connection as sys should be as SYSDBA or SYSOPER 用户名 :sys 密码:  自己设定的database:ORCLconnect as : ...