Android5 Service通信(有问题...)

有问题...

act:

package com.example.service3;

import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

public class Service3Activity extends Activity {
private Intent intent = new Intent();
private Service3.mBinder binder;
private ServiceConnection sConnection = new ServiceConnection() {

public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--ServiceDisconnected--");
}

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--ServiceConnected--");
binder = (Service3.mBinder)service;
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service3);

intent.setAction("android.service");

Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
bindService(intent, sConnection, BIND_AUTO_CREATE);
}
});

Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(sConnection);
}
});

Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Service3Activity.this,

"Service鐨刢ounter" +
binder.getCounter(),

Toast.LENGTH_LONG).show();

}
});
}

}

service:

package com.example.service3;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class Service3 extends Service {
private int counter = 0;
private boolean bRunning = true;
private mBinder binder = new mBinder();

public class mBinder extends Binder{
public int getCounter(){
return counter;
}
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

new Thread(new Runnable() {

public void run() {
// TODO Auto-generated method stub
while (!bRunning) {
try {
Thread.sleep(1000);
} catch (Exception e) {}
counter++;
}
}
}) .start();

// Log.v("counter", "鍊间负锛? + binder.getCounter());
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
bRunning = false;
}

}

androidxml:

</activity>

<service android:name="Service3">
<intent-filter>
<action android:name="android.service"/>

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>

layout:

<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" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="启动Service" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="停止Service" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="获取数据" />

</RelativeLayout>

时间: 2024-08-01 07:37:24

Android5 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并传递数据