先点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. C Primer Plus(第五版)5

    第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...

  2. oracle行列转换函数的使用

    oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...

  3. NOIP201305转圈游戏

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

  4. 启用 CORS 来解决这个问题(ajax跨域请求)

    <input type="file" name="btn_Upload" value="上传" id="btn_Upload ...

  5. 关于 profile文件(转)

    登录shell执行了两个特殊文件, 1个是:\etc\profile, 这个文件由系统管理员设置,通常做一些如检查是否有邮件,设置默认的创建文件的掩码,给某些表转到处变量赋值,已经任何管理员希望每当用 ...

  6. Laxcus大数据管理系统2.0(9)- 第七章 分布任务组件

    第七章 分布任务组件 Laxcus 2.0版本的分布任务组件,是在1.x版本的基础上,重新整合中间件和分布计算技术,按照新增加的功能,设计的一套新的.分布状态下运行的数据计算组件和数据构建组件,以及依 ...

  7. ORA-00933: SQL command not properly ended

    今天写了一个小的SQL语句类似下面的这句: UPDATE A SET ID=B.ID FROM A,B WHERE A.NAME=B.NAME 在执行时居然报了“ORA-00933: SQL comm ...

  8. 洛谷P1467 循环数 Runaround Numbers

    P1467 循环数 Runaround Numbers 89通过 233提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 循环数是 ...

  9. Hibernate 问题,在执行Query session.createQuery(hql) 报错误

    在配置文件中加入 <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classi ...

  10. flask-admin

    初始化 class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=No ...