先点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. Nginx作为静态内容服务器(Windows环境)

    1.简单安装 1)下载 http://nginx.org/en/download.html 2)解压后的路径 E:\Study\nginx\nginx-1.7.6 3)执行nginx.exe,访问ht ...

  2. NOIP201305转圈游戏

    2016.1.25 试题描述 有n个小伙伴(编号从0到n-1)围坐一圈玩游戏.按照顺时针方向给n个位置编号,从0到n-1.最初,第0号小伙伴在第0号位置,第1号小伙伴在第1号位置,……,依此类推.   ...

  3. 菜鸟-手把手教你把Acegi应用到实际项目中(12)-Run-As认证服务

    有这样一些场合,系统用户必须以其他角色身份去操作某些资源.例如,用户A要访问资源B,而用户A拥有的角色为AUTH_USER,资源B访问的角色必须为AUTH_RUN_AS_DATE,那么此时就必须使用户 ...

  4. 《Code Complete》ch.24 重构

    WHAT? 重构(refactoring),Martin Fowler将其定义为“在不改变软件外部行为的前提下,对其内部结构进行改变,使之更容易理解并便于修改”. WHY? 神话:一个管理很完善的软件 ...

  5. 酷我音乐API

    今天把酷我音乐API分享给大家: 歌曲搜索API:http://search.kuwo.cn/r.s?all={0}&ft=music& itemset=web_2013&cl ...

  6. vs 中无法加载项目的解决方案

    有时,我们在vs 中打开工程项目时,会出现这样的错误,项目无法加载,解决方案如下: 错误提示: 解决方案: 在vs 中单独打开 工程配置文件 ,然后修改配置,将红圈处的内容注释掉: 重新加载项目:

  7. AX函数,将EXCEL列号转为列名

    str GetExcelColName( int i_col) { int j; str ret; int v_div,v_mod; str tmp1,tmp2; int i_col_ascii; ; ...

  8. JS常用的设计模式(10)——模版方法模式

    模式方法是预先定义一组算法,先把算法的不变部分抽象到父类,再将另外一些可变的步骤延迟到子类去实现.听起来有点像工厂模式( 非前面说过的简单工厂模式 ). 最大的区别是,工厂模式的意图是根据子类的实现最 ...

  9. 002vim常用命令

    1.命令行模式:刚进入vim时的模式,该模式下可以移动光标进行浏览,可以进行整行删除等操作,但无法编辑文字,该模式下的功能键有: (1)yy:复制当前光标所在行 (2)[n]yy:n为数字,复制当前光 ...

  10. 二十、ValueStack的常用方法

    二十.ValueStack的常用方法 void set(String key,Object value):先获取根栈栈顶的Map,如果不存在,压入一个新的Map public String execu ...