通过bind实现activity与service的交互
先点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的交互的更多相关文章
- Activity与Service之间交互并播放歌曲的实现代码
Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...
- Activity与Service数据交互:Binder、bindService的用法
package com.lixu.jiaohu; import com.lixu.jiaohu.MyAppService.Mybind; import android.app.Activity; im ...
- Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
- Android四大组件应用系列——Activity与Service交互实现APK下载
Servic与Activity相比它没有界面,主要是在后台执行一些任务,Service有两种启动方法startService()和bindService(),startService方式Service ...
- Activity与Service进行数据交互
Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...
- Android-Start方式和Bind方式混合开启Service
Android-Start方式和Bind方式混合开启Service 需求如下 需要开发一个音乐APP,需要满足以下的需求: 当退出所有的Activity后仍然能够播放音乐 能够控制音乐的播放比如说,暂 ...
- Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者
1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- Android Activity、Service、BroadcastReceiver 的生命周期
Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...
随机推荐
- Words in Coding Theory
Lemma d(x.y) wt(c,0) self-dual self-orthogonal even prime wt(C) matrix column permute permutation ge ...
- nginx 添加perl
首先,要知道你原安装的nginx版本,以及原来安装的模块,用nginx -V即可 /usr/sbin/nginx -V 结尾处的--add-module 重新安装时这里可以去掉, 然后去官网下载一个相 ...
- How to Resize a Datafile (文档 ID 1029252.6)
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 and laterInformation in this docu ...
- (转)由Uploadify插件想到的Flash无法传递Session和Cookie的问题解决
在ASP.NET MVC3中使用Uploadify上传文件时发现,在后台需要验证登录状态的时候,Uploadify根本无法完成验证,因此,在后台只能使用非验证状态进行文件上传 —— 众所周知,这无异于 ...
- JavaFX Application应用实例
下面代码演示的是JavaFX进程命令行参数的实例.大家可以参阅一下. /*原文地址:http://www.manongjc.com/article/134.html */ import java.ut ...
- php mysql_select_db实例
在php中,我们经常需要连接数据库和操作数据库, 在操作数据库之前,我们需要选择一个数据库,本文章通过实例向小伙伴们介绍php mysql_select_db选择数据库的使用方法. 首先介绍一下mys ...
- 解决Git报错:The current branch is not configured for pull No value for key branch.master.merge found in configuration
1.在本地工程目录找到config文件(我的是在D:\git\demo\.git):2.修改config文件内容为: [core] repositoryformatversion = 0 filemo ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem I
Problem I Interesting Calculator There is an interesting calculator. It has 3 rows of button. Row 1: ...
- MSDN(电驴)
主站: http://msdn.itellyou.cn/ 辅站: http://msdn.ez58.net/
- C and C++ 如何嵌套使用
1. 要知道extern "C"的含义: (1) extern是C/C++中表明函数和全局变量作用范围的关键字, 该关键字表明其申明的函数和变量可以在本模块或者其他模块中使用. ( ...