service(2)

package org.lxh.demo;

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

public class MyServiceDemo extends Activity {
	private Button start; 										// 定义按钮
	private Button stop; 										// 定义按钮
	private Button bind;										// 定义按钮
	private Button unbind;										// 定义按钮
	private ServiceConnection serviceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, 
				IBinder service) { 								// 连接到Service
			try {
				System.out.println("### Service Connect Success. service = "
						+ service.getInterfaceDescriptor());
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		@Override
		public void onServiceDisconnected(ComponentName name) { // 与Service断开连接
			System.out.println("### Service Connect Failure.");
		}
	}; 															// 接收服务状态
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);					// 调用布局文件
		this.start = (Button) super.findViewById(R.id.start); 	// 取得组件
		this.stop = (Button) super.findViewById(R.id.stop); 	// 取得组件
		this.bind = (Button) super.findViewById(R.id.bind); 	// 取得组件
		this.unbind = (Button) super.findViewById(R.id.unbind); // 取得组件
		this.start.setOnClickListener(new StartOnClickListenerImpl()) ;	// 单击事件
		this.stop.setOnClickListener(new StopOnClickListenerImpl()) ;	// 单击事件
		this.bind.setOnClickListener(new BindOnClickListenerImpl()) ;	// 单击事件
		this.unbind.setOnClickListener(new UnbindOnClickListenerImpl()) ;	// 单击事件
	}
	private class StartOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			MyServiceDemo.this.startService(new Intent(
					MyServiceDemo.this, MyServiceUtil.class));	// 启动Service
		}
	}
	private class StopOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			MyServiceDemo.this.stopService(new Intent(
					MyServiceDemo.this, MyServiceUtil.class));	// 停止Service
		}
	}
	private class BindOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			MyServiceDemo.this.bindService(new Intent(MyServiceDemo.this,
					MyServiceUtil.class), MyServiceDemo.this.serviceConnection,
					Context.BIND_AUTO_CREATE); 					// 绑定Servic
		}
	}
	private class UnbindOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			MyServiceDemo.this
				.unbindService(MyServiceDemo.this.serviceConnection);// 取消Service绑定
		}
	}
}
package org.lxh.demo;

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

public class MyServiceUtil extends Service {			// 必须继承Service
	private IBinder myBinder = new Binder(){
		@Override
		public String getInterfaceDescriptor() {	// 取得接口描述信息
			return "MyServiceUtil class.";				// 返回Service类的名称
		}
	} ;
	@Override
	public IBinder onBind(Intent intent) {			// 绑定时触发
		System.out.println("*** Service onBind() Intent = " + intent) ;
		return myBinder;
	}
	@Override
	public void onRebind(Intent intent) {			// 重新绑定时触发
		System.out.println("*** Service onRebind() Intent = " + intent);
		super.onRebind(intent);
	}
	@Override
	public boolean onUnbind(Intent intent) {		// 解除绑定时触发
		System.out.println("*** Service onUnbind() Intent = " + intent);
		return super.onUnbind(intent);
	}
	@Override
	public void onCreate() {						// 创建时触发
		System.out.println("*** Service onCreate()");
		super.onCreate();
	}
	@Override
	public void onDestroy() {						// 销毁时触发
		System.out.println("*** Service onDestroy()");
		super.onDestroy();
	}
	@Override
	public int onStartCommand(Intent intent, 
			int flags, int startId) {				// 启动时触发
		System.out.println("*** Service onStartCommand() Intent = " + intent);
		return Service.START_CONTINUATION_MASK;
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button
		android:id="@+id/start"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="启动Service" />
	<Button
		android:id="@+id/stop"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="停止Service" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MyServiceDemo" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<service android:name=".MyServiceUtil" />
	</application>
</manifest>

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++实现的代码,需要注意的是,本代码中使用了C++11的特性,如std::regex,需要开启C++11支持: ```c++ #include <iostream> #include <string> #include <regex> #include <unordered_map> int main() { std::string config = "service1.host=192.168.1.1;service1.port=8080;service2.host=192.168.1.2;service2.port=8081;"; std::unordered_map<std::string, std::unordered_map<std::string, std::string>> service_configs; // 匹配子服务名称和配置项 std::regex pattern(R"(([^.;]+)\.([^.;]+)=([^.;]+);?)"); std::smatch match; // 循环匹配配置项 while (std::regex_search(config, match, pattern)) { // 判断匹配是否成功 if (match.size() == 4) { // 获取子服务名称,配置项名称和配置项值 std::string service_name = match[1].str(); std::string config_name = match[2].str(); std::string config_value = match[3].str(); // 判断子服务是否已存在,不存在则创建 if (service_configs.find(service_name) == service_configs.end()) { service_configs[service_name] = std::unordered_map<std::string, std::string>(); } // 添加配置项 service_configs[service_name][config_name] = config_value; } // 更新配置字符串 config = match.suffix().str(); } // 输出结果 for (const auto& [service_name, service_config] : service_configs) { std::cout << service_name << ":\n"; for (const auto& [config_name, config_value] : service_config) { std::cout << "\t" << config_name << " = " << config_value << "\n"; } } return 0; } ``` 输出结果为: ``` service1: host = 192.168.1.1 port = 8080 service2: host = 192.168.1.2 port = 8081 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值