将数据从BroadcastReceiver传递到扩展runnable的类

时间:2014-11-29 10:19:27

标签: android broadcastreceiver

我的目标是创建一种从广播接收器获取数据并通过套接字发送数据的方法。我知道如何发送它,但我如何从BroadcastReceiver获取数据?我想在这堂课上收到数据:

public class ClientThread implements Runnable{
    Socket socket;
    public final String TAG = "CLIENT";
    ObjectOutputStream os;
    TextView text;
    Handler handler;
    AppHelper helperClass;
    Activity mActivity;
    Context mContext;
    public ClientThread(Activity mActivity,TextView text,Context context) {
        // TODO Auto-generated constructor stub
         this.text=text;
         this.mContext=context;;
         helperClass = new AppHelper(context, mActivity);
         handler = new Handler();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            while (true) {
                socket = new Socket("192.168.1.10", 9000);

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        text.setText("Connected.");
                        try {
                            os = new ObjectOutputStream(socket
                                    .getOutputStream());
                        } catch (Exception e) {
                            text.setText("Output stream. smth wrong");
                            Log.i(TAG, "Output stream. smth wrong");
                        }
                    }
                });

                try {
                    ObjectInputStream in = new ObjectInputStream(
                            socket.getInputStream());
                    String line = null;
                    while ((line = in.readUTF().toString()) != null) {
                        Log.d("ServerActivity", line);
                        final String mesg = line;
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (mesg.contains("getcontacts")) {
                                    try {
                                        sendContacts();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                } else if (mesg.contains("getmsg")) {
                                    getMessages();

                                } else if (mesg.contains("sendmsg")) {
                                    sendMessage(mesg);
                                } else {

                                    // DO WHATEVER YOU WANT TO THE FRONT END
                                    // THIS IS WHERE YOU CAN BE CREATIVE
                                    text.append(mesg + "\n");
                                }
                            }

                        });
                    }
                    break;
                } catch (Exception e) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            text.setText("Oops. Connection interrupted. Please reconnect your phones.");
                        }
                    });
                    e.printStackTrace();
                }
            }

        } catch (Exception x) {
        }
    }

    public void receivedMessage(String sender, String message) {

    }

    private void sendMessage(String s) {
        String[] x = s.split(" ");
        int lenght = x.length;
        String message = x[2];
        for (int i = 3; i < x.length; i++) {
            message = message + " " + x[i];
        }
        System.out.println(message);

        SmsManager smsManager = SmsManager.getDefault();
        System.out.println("sending message");
        smsManager.sendTextMessage(x[1], null, message, null, null);
        System.out.println("message send");
    }

    private void sendContacts() throws IOException {

        final List<Person> list = helperClass.getContacts();

        final String json = new Gson().toJson(list);
        Log.i(TAG, "lenght " + json.length());

        handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    os.writeObject(json);
                    os.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "Sending Contact list has failed");
                    e.printStackTrace();
                }
            }
        });
    }

    private void getMessages() {
        // TODO Auto-generated method stub
        ProgressTask task = new ProgressTask();
        ProgressTask.execute(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    List<Sms> sms = helperClass.getAllSms();
                    final String json = new Gson().toJson(sms);
                    os.writeObject(json);
                    os.flush();
                    System.out.println("List of messages send");

                } catch (Exception e) {
                    Log.e(TAG, "Sending Contact list has failed");
                }
            }
        });


    }


}

1 个答案:

答案 0 :(得分:1)

您可以使用接口作为此runnable的侦听器。

  1. 创建一个监听器界面
  2. 在应用程序上下文中声明接口变量。
  3. 在Runnable类中实例化并实现接口。
  4. 在BroadcastReciever中,调用该接口的方法。
  5. 例如:

    Class A extends Application{
         public Listener listener;
    
    }`
    
    interface Listener{
        public void yourmethod();
    }
    

    在您的clientthread方法中,按如下方式实例化侦听器:

    ((A)context.getApplication()).listener = new Listener(){
        @override
        public void yourmethod(){
           // your implementation goes here
        }
    }
    

    在您的广播接收器中。

    ((A)context.getApplication()).listener.yourmethod();