0

I have created a TCP socket connection using Android service. The activity starts when the button is clicked in the MainActivity. And it works properly and closes the connection when the Activity is destroyed. MyService.java

public class MyService extends Service{
    public static Socket clientsocket;
    public static PrintWriter printer;
    SendMessage sender;

    @Override
    public void onCreate() {
    Toast.makeText(this, "Created", Toast.LENGTH_LONG).show();
    super.onCreate();
    }

    @Override
    public void onDestroy() {
    Toast.makeText(this, "Stoped", Toast.LENGTH_LONG).show();
    if(clientsocket!=null){
            try{
            clientsocket.close(); 
            Toast.makeText(this, "Socket Closed", Toast.LENGTH_LONG).show();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Running", Toast.LENGTH_LONG).show();
        SendMessage sender=new SendMessage();
        sender.execute();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    class SendMessage extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            try {
                clientsocket = new Socket("192.168.237.1", 6666);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if(clientsocket!=null){
                Toast.makeText(MyService.this, "Connected", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(MyService.this, "Lost Connection", Toast.LENGTH_LONG).show();
            }
            super.onPostExecute(result);
        }

    }
}

MainActivity.java

public class MainActivity extends Activity {

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

    }

    public void start(View v){
        startService(new Intent(this,MyService.class));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
    }

    class PrintMessage extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... arg0) {
            return null;
        }
    }

    @Override
    protected void onDestroy() {
        stopService(new Intent(this,MyService.class));
        super.onDestroy();
    } 
}

Now I have some questions:

  1. Is it possible to create PrintWriter objects in MainActivity using Socket object (clientsocket) of service?
  2. Is it possible to pass values from MainActivity to service(i.e is IP address and port)?
  3. Is it possible to check socket connection in MainActivity(i.e socket.isConnected)?

Please help .

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Suroor Ahmmad
  • 1,110
  • 4
  • 23
  • 33

1 Answers1

0

1.Is it possible to create PrintWriter objects in MainActivity using Socket object (clientsocket) of service?

You can use messager and send your object in bundle from service to activity.

see this ho to send data from service to activity: Send JSON data from a service to UI in Android

2.Is it possible to pass values from MainActivity to service(i.e is IP address and port)? yes you can pass ip/port via an intent and get them in onStartcomant of service

i.e

i.putExtra(name, value);

i.getStringExtra(name)

3.Is it possible to check socket connection in MainActivity(i.e socket.isConnected)?

yes you can send any update from service to mail activity via messanger

for sending printwriter:

Message msg = Message.obtain();
Bundle data = new Bundle();
data.putSerializable("key", new printwriterHolder(mPrintWriter));

//class to hold print writer
class printwriterHolder implements Serializable{
    PrintWriter mPrintWriter;

    public printwriterHolder(PrintWriter mPrintWriter){
        this.mPrintWriter=mPrintWriter;
    }

    public PrintWriter getPrintWriter(){

        return mPrintWriter;
    }
}
Community
  • 1
  • 1
H4SN
  • 1,482
  • 3
  • 24
  • 43
  • you may need to extend PrintWriter and implement Serializable in order to bundle it. – H4SN Dec 11 '14 at 07:46
  • can you please explain it. I got to know `Serializable` should be implemented if we want to send Objects. but is it necessary for bundling also? and how to extend `PrintWriter` when I want to extend `Activity` – Suroor Ahmmad Dec 11 '14 at 08:49
  • I think you have misinterpreted my questions. I want to send Socket object in this case it is cliensocket from MyService.java class to MainActivity.java – Suroor Ahmmad Dec 11 '14 at 17:25