先点bind按钮实现onCreate,在点击start给service传值(get方法)

xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.bind.MainActivity" > <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:text="start" /> <Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:layout_marginTop="65dp"
android:text="bind" /> <Button
android:id="@+id/dedao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bind"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:text="取值" /> </RelativeLayout>

布局

记住注册service

MainActivity:

 package com.zzw.bind;

 import com.zzw.bind.MyAppService.MyBinder;

 import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity {
private ServiceConnection sc;
private MyAppService myAppService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder mBinder = (MyBinder) service;
myAppService = mBinder.getService();
}
};
findViewById(R.id.start).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
startService();
}
});
findViewById(R.id.bind).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
bindService();
}
});
findViewById(R.id.dedao).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String str = myAppService.getServiceValue();
Log.d("得到的值", str);
}
}); } private void startService() {
myAppService.setServiceValue("hello"); Intent intent = new Intent(this, MyAppService.class);
startService(intent);
} private void bindService() {
Intent intent = new Intent(this, MyAppService.class);
bindService(intent, sc, Service.BIND_AUTO_CREATE);
} @Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent(this, MyAppService.class);
stopService(intent);
Log.d("MainActivity", "onDestroy");
} }

Service:

 package com.zzw.bind;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class MyAppService extends Service {
private String str; @Override
public void onCreate() {
super.onCreate();
Log.d("MyAppService","onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.str=str+",world";
Log.d("onStartCommand",str);
return super.onStartCommand(intent, flags, startId);
} public String getServiceValue() {
Log.d("getServiceValue", str);
return str;
} public void setServiceValue(String str) {
Log.d("setServiceValue", str);
this.str = str;
} @Override
public IBinder onBind(Intent intent) {
Log.d("onBind", "======");
return new MyBinder();
} public class MyBinder extends Binder {
public MyAppService getService() {
Log.d("getService", "====");
return MyAppService.this;
}
} @Override
public void onDestroy() {
Log.d("MyAppService", "onDestroy");
super.onDestroy();
} @Override
public boolean onUnbind(Intent intent) {
Log.d("MyAppService", "onUnbind");
return super.onUnbind(intent);
} }

通过bind实现activity与service的交互的更多相关文章

  1. Activity与Service之间交互并播放歌曲的实现代码

    Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...

  2. Activity与Service数据交互:Binder、bindService的用法

    package com.lixu.jiaohu; import com.lixu.jiaohu.MyAppService.Mybind; import android.app.Activity; im ...

  3. Android Activity与Service的交互方式

    参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...

  4. Android四大组件应用系列——Activity与Service交互实现APK下载

    Servic与Activity相比它没有界面,主要是在后台执行一些任务,Service有两种启动方法startService()和bindService(),startService方式Service ...

  5. Activity与Service进行数据交互

    Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...

  6. Android-Start方式和Bind方式混合开启Service

    Android-Start方式和Bind方式混合开启Service 需求如下 需要开发一个音乐APP,需要满足以下的需求: 当退出所有的Activity后仍然能够播放音乐 能够控制音乐的播放比如说,暂 ...

  7. Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者

    1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  8. 201709012工作日记--activity与service的通信机制

    service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...

  9. Android Activity、Service、BroadcastReceiver 的生命周期

    Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...

随机推荐

  1. [SQL]sql介绍

    SQL语言概述 结构化查询语言(Structured Query Language,简称SQL)是一种介于关系代数与关系演算之间的语言,是一种用来与关系数据库管理系统通信的标准计算机语言.其功能包括数 ...

  2. jdk集合常用方法分析之HashSet和TreeSet

    HashSet常用方法介绍 public boolean add(E e) public boolean isEmpty() void clear() public Iterator<E> ...

  3. 有关OpenCV1.0中GUI命令的几个函数学习总结

    1.修改窗口背景色或者光标形状 在OpenCV1.0版本利用函数int cvNamedWindow( const char* name, int flags )初始化创建一个窗口后,窗口的背景色是灰色 ...

  4. 第4章 sed命令

    1 sed命令基本用法 sed(stream editor)是流编辑器,可对文本文件和标准输入进行编辑: sed只是对缓冲区中原始文件的副本进行编辑,并不编辑原始的文件,如果需要保存改动内容,可以选择 ...

  5. button的type属性

    今天为看懂一段js代码纠结了很久,搞不明白数据是如何实现post,因为button没有规定属性,其次对submit事件没太搞明白.忽然想起默认属性这个概念,豁然开朗,啊~ 1.请始终为按钮规定 typ ...

  6. PAT1075. PAT Judge

    //终于A了,不难却觉着坑多的的题,注意-1的处理,感觉我是受memset置0的束缚了,可以把初试成绩置-1.就不用debug怎么久,注意对于-1的处理,不然漏洞百出 #include<cstd ...

  7. word-wrap:break-word和word-break:break-all的小小比较

    个人感觉word-break:break-all;的用法比word-wrap:break-word;要好点, 当然这是出于某些需求而言,下面简单说下word-break:break-all的优点 wo ...

  8. ifdown eth0 && idup eth0 ifdown --exclude=l0 -a && ifup --exclude=lo -a

  9. docker1.12在cento7里的跨多主机容器网络

    docker1.12在cento7里的跨多主机容器网络

  10. 雷兽的数据库CAP乱谈之(一)阐述

    今天有人问我cap,找了https://my.oschina.net/lilw/blog/169776这片文字, 下面是cap那篇文字的解释: 所谓CAP理论,即: Cosistency       ...