service通信

添加按钮

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="默认信息"
android:id="@+id/edtData" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:id="@+id/btnStartService" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:id="@+id/btnStopService" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:id="@+id/btnBindService" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除绑定"
android:id="@+id/btnUnBindService" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同步数据"
android:id="@+id/btnSyncData" />

添加MainActivity文件的方法

findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnBindService).setOnClickListener(this);
findViewById(R.id.btnSyncData).setOnClickListener(this);

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnStartService:
intent = new Intent(this,MyService.class);

intent.putExtra("data", edtData);
startService(intent);
break;
case R.id.btnStopService:

stopService(intent);
break;
case R.id.btnBindService:
bindService(intent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnSyncData:
edtData=((EditText)findViewById(R.id.edtData)).getText().toString();
binder.setData(edtData);
break;

default:
break;
}

}

myService文件

public class Binder extends android.os.Binder{
public void setData(String data){
MyService.this.data=data;
}

}

public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
running = true;
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (running) {
System.out.println("服务正在运行:"+data);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}.start();
System.out.println("service onCreate");
}

mainActivity文件

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
if (binder == null){
binder = (Binder) service;
}

}

时间: 2024-12-17 18:41:54

service通信的相关文章

Android之Service通信

一.本地Service通信 LocalService /** * Created by lsj on 2015/8/29. * 这是一个LocalService */ public class StrReplaceService extends Service { private final String TAG="StrReplaceService" ; public class MyBinder extends Binder{ public StrReplaceService ge

【学习笔记】Android中Service通信

一.Service的生命周期(onCreate onStart onDestroy onBind ) 1). 被启动的服务的生命周期:如果一个Service被某个Activity 调用 Context.startService 方法启动,那么不管是否有Activity使用bindService绑定或unbindService解除绑定到该Service,该Service都在后台运行.如果一个Service被startService 方法多次启动,那么onCreate方法只会调用一次,onStart

IIS 为应用程序池提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误的解决方法

系统环境:Windows Server 2008 R2 64位, IIS 7.0 错误信息: 为应用程序池提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误.该进程ID为. 应用程序池将被自动禁用,原因是为此应用程序池提供服务的进程中出现一系列错误. 导致网站不能访问,出现 503 错误,服务不可用,Service Unavailable. 解决的方法: 1. 将应用程序池设置为 经典 Classic 模式(如果是纯.NET应用,此步骤可

为应用程序池“XX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误

场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池"XX"提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误 使用windbg在崩溃时进行分析,找到引发崩溃的堆栈的最顶部信息进行搜索,找到两个补丁包安装上 用于搜索的顶部异常信息为:IOCompletionThunk.UnhandledExceptionFra

与Service通信的简单总结

为了方便描述,以下用"服务端"代表Service,用"客户端"代表启动Service的一端.与Service通信的方法有很多种,比如:. 直接启动Service,通过Intent传入参数. 通过Binder绑定Service进行直接通信,绑定Service也有三种不同的实现分别用于不同的场景 * 实现一:如果服务只在自己的程序里用,并且在同一个进程中,那么可以通过在Service里扩展Binder类并提供相关接口,这样当客户端首次和服务端绑定时,服务端可以通过onB

(六)Android中Service通信

一.启动Service并传递参数 传递参数时只需在startService启动的Intent中传入数据便可,接收参数时可在onStartCommand函数中通过读取第一个参数Intent的内容来实现 1.MainActivity.java package com.example.shiyanshi.serviceconnected; import android.app.Activity;import android.content.Intent;import android.os.Bundle

Android之Service通信-(2)

一.Service通过IBinder与Activity进行通信 在Service中进行下载 Service package chuiyuan.lsj.androidjava.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.wi

Service通信详解

1.使用Intent进行异步通讯 在Service任务一旦完成后,就发送广播.开发者只需要实现一个BroadcastReceiver来监听响应既可. Activity.startService启动intentService,intentService完成任务后sendBroadcast()发送广播,BroadcastReceiver.startActivity()通知Activity操作结果. 在任务结束后调用sendBroadcast(new Intent(Action)); 然后在广播中通知

【三】8 与Service通信

内容包括: 启动Service并传递数据 绑定Service进行通信(上) 绑定Service进行通信(下) [一]启动Service并传递数据

Android之Activity与Service通信

一.当Acitivity和Service处于同一个Application和进程时,通过继承Binder类来实现. 当一个Activity绑定到一个Service上时,它负责维护Service实例的引用,允许你对正在运行的Service进行一些方法调用.比如你后台有一个播放背景音乐的Service,这时就可以用这种方式来进行通信. 代码如下: /*************************Service代码****************************************/ p