service例子


public class SendDataToServer extends Service{

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
// 开始执行后台任务
}
}).start();
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
//与activity 绑定
class MyBinder extends Binder {

public void startDownload() {
// 执行具体的下载任务
}
}


public class MainActivity extends Activity implements OnClickListener {

private Button startService;

private Button stopService;

private Button bindService;

private Button unbindService;

private MyService.MyBinder myBinder;

private ServiceConnection connection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.startDownload();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(connection);
break;
default:
break;
}
}

}



<service android:name="com.example.servicetest.MyService" >
好的,下面是一个基于Apache CXF的web service例子: 1. 首先,创建一个Maven项目,并添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.7</version> </dependency> </dependencies> ``` 2. 创建一个接口,并定义web service的方法: ```java @WebService public interface GreetingService { @WebMethod String sayHello(String name); } ``` 3. 创建一个实现类,并实现接口中定义的方法: ```java @Service @WebService(endpointInterface = "com.example.demo.GreetingService") public class GreetingServiceImpl implements GreetingService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 在配置文件中配置CXF: ```yaml cxf: servlet: load-on-startup: 1 url-pattern: /services/* jaxws: properties: javax.xml.ws.wsdl.service: GreetingService javax.xml.ws.wsdl.port: GreetingServicePort ``` 5. 部署应用程序并启动服务器,访问以下URL即可访问web service: ``` http://localhost:8080/services/GreetingService?wsdl ``` 6. 测试web service: ```java public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(GreetingService.class); factory.setAddress("http://localhost:8080/services/GreetingService"); GreetingService client = (GreetingService) factory.create(); String result = client.sayHello("World"); System.out.println(result); } } ``` 以上就是一个简单的基于Apache CXF的web service例子
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值