Day17_server作业

本文深入探讨了Android应用中Server间传值的两种主要方法:Messenger信使传值与AIDL传值。通过实例详细解析了Messenger如何实现server与client之间的消息传递,以及AIDL如何在不同进程间进行数据交换,为开发者提供了实践指南。

Server之间传值的奥秘

第一种 Messenger 信使传值

server 传值
package com.example.bpp_server.server;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class MyService extends Service {
    public MyService() {
    }

    Messenger messenger = new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {

            Bundle bundle = msg.getData();
            String str = bundle.getString("str");

            Log.i("---", "客户端的:"+str);

            //发送至客户端
            Messenger replyTo = msg.replyTo;
            Message obtain = Message.obtain();
            Bundle bundle1 = new Bundle();
            bundle1.putString("back","我也爱你,客户端");
            obtain.setData(bundle1);
            try {
                replyTo.send(obtain);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    });

    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}

client 中
package com.example.bpp_client;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setAction("bpp.server");
        intent.setPackage("com.example.bpp_server");

        final Messenger clientMessenger = new Messenger(new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle data = msg.getData();
                String back = data.getString("back");
                Log.i("---", "handleMessage: 服务器的:"+back);
            }
        });


        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                Messenger messenger = new Messenger(service);

                Message obtain = Message.obtain();

                Bundle bundle = new Bundle();
                bundle.putString("str","我爱你服务器");
                obtain.setData(bundle);

                obtain.replyTo = clientMessenger;
                try {
                    messenger.send(obtain);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };


        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
}

第二种 AIDL 传值

首先在main Activity中创建一个自定义名(一般叫aidl)的包,其中新建IMyAIDLInterface

// IMyAidlInterface.aidl
package com.example.app_aidl_a;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    int add(int a,int b);
    void callShow();
}

之后是在server中

package com.example.app_aidl_a;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    IBinder iBinder = new IMyAidlInterface.Stub() {
        @Override
        public int add(int a, int b) throws RemoteException {
            return a+b;
        }

        @Override
        public void callShow() throws RemoteException {
            show();
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }

    public void show(){
        Log.i("---", "show: "+"我被调用了");
    }
}

之后再client客户端中
要创建一个与服务器同包同名的 aidl

// IMyAidlInterface.aidl
package com.example.app_aidl_a;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    int add(int a,int b);
    void callShow();
}

只后在client的MainActivity中

package com.example.app_aidl_b;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.app_aidl_a.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setPackage("com.example.app_aidl_a");
        intent.setAction("pan.test");

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int add = iMyAidlInterface.add(5, 3);
                    iMyAidlInterface.callShow();
                    Toast.makeText(MainActivity.this, "结果是:->"+add, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
}

这样就差不多了

SQL Server 作业可以循环执行存储过程,这通常是通过 SQL Server 代理作业来实现的。SQL Server 代理作业可以包含多个作业步骤,每个步骤可以是 Transact-SQL(T-SQL)语句,包括存储过程和扩展存储过程[^2]。 要创建一个循环执行存储过程的 SQL Server 代理作业,你可以按照以下步骤操作: 1. 创建一个存储过程,该过程将执行所需的任务。例如,下面的存储过程 `sp_data_move` 接受多个参数,并在过程中声明了一些变量用于后续处理: ```sql if(exists(select * from sysobjects where name='sp_data_move' )) drop proc sp_data_move go --声明存储过程 create procedure sp_data_move @staffChannelId varchar(36), @agencyChannelId varchar(36), @medicalChanneId varchar(36) as --声明变量 declare @lms_id varchar(50), @lms_realname varchar(50), ... ``` 2. 创建 SQL Server 代理作业,并在作业中添加一个或多个步骤来调用上述存储过程。你还可以设置作业的调度信息,以确定何时以及如何频繁地执行作业。 3. 如果需要在存储过程中实现循环逻辑,可以使用 `WHILE` 循环。例如,下面的代码片段展示了如何打印两个给定日期之间的所有日期: ```sql DECLARE @date_start DATE; DECLARE @date_end DATE; DECLARE @loop_date DATE; SET @date_start = '2020/11/11'; SET @date_end = '2020/12/12'; SET @loop_date = @date_start; WHILE @loop_date <= @date_end BEGIN PRINT @loop_date; SET @loop_date = DATEADD(DAY, 1, @loop_date); END; ``` 通过在存储过程中加入这样的循环逻辑,或者通过设置 SQL Server 代理作业的调度,可以实现对存储过程的循环执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值