-1

Im a newbie to android. i want to create an application that sends the user1 location to the user2 using ServerSocket.

public class ToMain extends Activity {

    TextView info, infoip, msg;
    String message ="";
    ServerSocket serverSocket;
    String s="",t="";
    public Double b,c;
    public int i;

    private class SocketServerThread extends Thread {

            static final int SocketServerPORT = 8080;
            int count = 0;

            @Override
            public void run() {
                Socket socket = null;
                DataInputStream dataInputStream = null;
                DataOutputStream dataOutputStream = null;

                try {
                    serverSocket = new ServerSocket(SocketServerPORT);

                    ToMain.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            info.setText("I'm waiting here: "
                                    + serverSocket.getLocalPort());
                        }
                    });

                    while (true) {


                        socket = serverSocket.accept();
                        dataInputStream = new DataInputStream(
                                socket.getInputStream());
                        dataOutputStream = new DataOutputStream(
                                socket.getOutputStream());

                        String messageFromClient = "";

                        //If no message sent from client, this code will block the program
                        messageFromClient = dataInputStream.readUTF();

I Split the Latitude and Longitude this way in the Server side

                    for(int j=0;j<i;j++){

                        if(messageFromClient.charAt(j)!='+' && bool==false){
                             s=s+messageFromClient.charAt(j);
                        }
                        else{
                            bool=true;
                        }
                        if(bool){
                            t=t+messageFromClient.charAt(j);
                        }
                    }

I Store in these two Variable

                    b = Double.parseDouble(s);
                    c = Double.parseDouble(t);
                    ToMain.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {


                        }
                    });

                    String msgReply = "Location Sent!" + count;
                    dataOutputStream.writeUTF(msgReply);

i want to send those variable from here to the main activity which contain the map

//  startActivity(intent);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            final String errMsg = e.toString();
            ToMain.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                }
            });

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

How do i pass a and b to the main activity.

4 Answers4

1

Pass data through intents.

Intent data = new Intent(this,SecondActivity.class);
data.putExtra("keyA",a);
data.putExtra("keyB",b);
startActivity(data);

In second activity in onCreate()
Intent intent = getIntent();
Double a = intent.getDoubleExtra("keyA",0);
Double b = intent.getDoubleExtra("keyB",0);
crashOveride
  • 839
  • 6
  • 12
0

To pass data from one Activity to another, you can use Extras in Intent.

Intent intent = new Intent("ACTIVITY_INTENT");
intent.putExtra("selection", position);
startActivity(intent);

You can send Binary, Text and multiple pieces as listed here, http://developer.android.com/training/sharing/send.html

To receive the data in the next activity, in the oncreate method of the activity,

extras = getIntent().getExtras();
int value = extras.getInt("selection");

In case if you want to persist the data, the simplest way to achieve this would be saving it in Shared Preferences.

Developer link has good information on how to implement this - http://developer.android.com/training/basics/data-storage/shared-preferences.html

To write the data,

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

To read the data,

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

all the best

Natarajan Raman
  • 606
  • 1
  • 4
  • 14
  • can You help me using SharedPreference. Because it can't used put extra – Kyrshanbor Pathaw Apr 01 '16 at 05:55
  • please check my updated answer. Put extra is only for intents to pass data from one activity to another. Share pref stores value you need as key, value pair. Please read about SP in the link I have given. It is easy to store and retrieve. – Natarajan Raman Apr 01 '16 at 05:59
0

Use Intent to pass data from one activity to another activity

Intent intent = new Intent(this,SecondActivity.class);
intent .putExtra("key1",value);
intent .putExtra("key2",value);
startActivity(intent );


In second activity in onCreate()

Intent intent = getIntent();
Double a = intent.getDoubleExtra("key1",0); // Notice 0 is the default value here
Double b = intent.getDoubleExtra("key2",0);
Adnan
  • 1,440
  • 2
  • 18
  • 38
0

In Android user interface is displayed through an activity. Activity is used to represent the data to user and allows user interaction. In an android application, we can have multiple activities and that can interact with each other. During activity interaction we might required to pass data from one activity to other.

Now, to pass data from one activity to other in android, you need to do the following :

You can send data from one actvity to another with an Intent. An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra() method. Data is passed as extras and are key/value pairs. The key is always a String. As value you can use the primitive data types int, float, chars, etc. We can also pass Parceable and Serializable objects from one activity to other.

Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);