先点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. poj 2632 Crashing Robots

    点击打开链接 Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6655   Accepted: ...

  2. (medium)LeetCode 240.Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. Xpath定位大全

    selenium使用Xpath定位之完整篇   其中有一片文章提到了xpath元素定位,但是该文章中有些并不能适应一些特殊与个性化的场景.在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需 ...

  4. dedecms不安全啊

    两个站都早被黑了,没心弄了.该注意的都注意了,除了没定期升级.不靠谱啊.开源软件的安全性是个大问题.

  5. LayoutInflater的实例化

    获得 LayoutInflater 实例的三种方式 1. LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutI ...

  6. pm剩余要看的内容

    1. thermal profiles tuning, customize thermal daemon for different thermal usage model 2.gas gauge d ...

  7. MFC学习 MFCActiveX控件

    例子包含 1. 重绘activex控件显示区域 在activex的ctrl类中ondraw中实现 2.添加修改activex控件属性(前景色, 背景色, 自定义属性),, 只要在类视图中展开libra ...

  8. python修改excel文件

    一.导入模块如图

  9. 代码生成器(CodeBuilder) 2 正式发布

    CodeBuilder是一个通过获取数据库表和字段定义,通过模板转换生成三层结构.实体模型等代码的工具. CodeBuilder第一版距今已过去4个年头了,第一版做的功能繁多,体积庞大,但是用起来不太 ...

  10. android 控件描边取消重叠

    今天写组件的时候用到了描边.可是两个组件放在一起时,描边会变重叠,使之变粗.就不是很美观了. 如何取消呢?网上查了好久没找到,然后就自己试了试,找到了解决方法,就在此记录一下,防止以后忘记. 很简单分 ...