其他分享
首页 > 其他分享> > 专高第十六天

专高第十六天

作者:互联网

专高第十六天

Service

服务端的代码:
MyServer代码

package com.example.message_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.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    private static final String TAG = "MyService";
    Messenger server_messager=new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {

            //接受客户端消息
            Bundle bundle=msg.getData();
            String message=bundle.getString("client_message");
            Toast.makeText(MyService.this, "服务器发来的消息:"+message, Toast.LENGTH_SHORT).show();
            //向客户端发送消息
            Messenger clientMessager = msg.replyTo;//用于获取客户端的信使
            Message messenge= Message.obtain();
            Bundle bundle1=new Bundle();
            bundle1.putString("server_message","客户端你是不是傻啊");
            messenge.setData(bundle1);
            try {
                clientMessager.send(messenge);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            super.handleMessage(msg);
        }
    });

    //TODO 返回server_messager的代理人,便于客户端获取服务端信使
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return server_messager.getBinder();
    }
    public MyService() {
    }
}

服务的清单文件

<service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.feng.1705A"></action>
            </intent-filter>
        </service>
     客户端的代码
package com.example.message_client;

import android.annotation.SuppressLint;
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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    //3.客户端的信使:负责接受服务器发来的消息
    private Messenger Clientmesssager=new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle=msg.getData();
            String message=bundle.getString("server_message");
            Toast.makeText(MainActivity.this, "接受到服务器发来的消息"+message, Toast.LENGTH_SHORT).show();
        }
    });
    // 4. 客户端发送消息给服务端
    private Messenger Servermessenger;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Servermessenger=new Messenger(service);//获取服务端信使
            Message message= Message.obtain();
            Bundle bundle=new Bundle();
            bundle.putString("client_message","你好啊服务器");
            message.setData(bundle);
            //非常重要的一句话
            message.replyTo = Clientmesssager;
            try {
                Servermessenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: 我说了啥 a");
        //1.绑定服务
        Intent intent=new Intent();
        intent.setAction("com.feng.1705A");
        intent.setPackage("com.example.message_server");
        bindService(intent,connection, Service.BIND_AUTO_CREATE);
    }
    //2.解除绑定
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}

标签:第十六,专高,Bundle,Messenger,import,android,os,message
来源: https://blog.csdn.net/Cys_yes/article/details/104759256