varunon9 / Remote-Control-PC

Control Laptop using Android. Remote control PC consists of android as well as desktop app written in Java to control laptop using phone.
MIT License
194 stars 97 forks source link

TextView not working when I changing activity . #39

Open ghost opened 3 years ago

ghost commented 3 years ago

I want to add a message option to send message from server to client with this project.

I have two Activities: MainActivity and SecondActivity. On MainActivity I click on a button and go to SecondActivity.

public void msg(View view) {
    Intent intent = new Intent(this, secondActivity.class);;
    startActivity(intent);
}

SecondActivity

 public class SecondActivity extends AppCompatActivity {

   private TextView info;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_SecondActivity);

     info = findViewById(R.id.terv);

     new Thread(new Te()).start();

  }

  private class Te implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {                 
                final String message = (String) MainActivity.objectInputStream.readObject();
                if (message != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            info.append(message);
                        }
                    });
                }
        } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
         }
   }
 }
 }

The TextView works smoothly and shows the message when opening the SecondActivity for the first time.

But for the second time, when I go back to MainActivity and open the SecondActivity, the TextView doesn't work.

How can I solve this?

varunon9 commented 3 years ago

on SecondActivity once you consume the first message (first time) using MainActivity.objectInputStream.readObject(), do you send it again from server (second time)?

You need to send it again if you want to consume it again.

ghost commented 3 years ago

on SecondActivity once you consume the first message (first time) using MainActivity.objectInputStream.readObject(), do you send it again from server (second time)?

You need to send it again if you want to consume it again.

I can't understand. Can you explain in detail ?

varunon9 commented 3 years ago

Server (Java), Client (Android app)-

  1. Server sent one message: You consumed it using MainActivity.objectInputStream.readObject() on SecondActivity
  2. You go back to SecondActivity and try to read the message again but since you have already read it previously it won't be there
  3. Server needs to send another message for client to be consumed it again or you can store the first message in client itself
  4. Another way can be - get message from server on demand i.e. client (SecondActivity) will ping server for message and since server will always be listening, it can fulfil the request
ghost commented 3 years ago

In MainActivity I get IP address and port number using edittext.

I want to send message only from server to client.

I changed this project's MainActivity to soc.java class.

soc.java

public class soc  {

public static Socket clientSocket = null;
public static ObjectInputStream objectInputStream = null;
public static ObjectOutputStream objectOutputStream = null;

public static void socketException() {
    if (soc.clientSocket != null) {
        try {
            soc.clientSocket.close();
            soc.objectOutputStream.close();
            soc.clientSocket = null;
        } catch(Exception e2) {
            e2.printStackTrace();
        }
    }
}

}

I want to send message only from server to client and view that message on SecondActivity.

SecondActivity

 public class SecondActivity extends AppCompatActivity {

   private TextView info;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_SecondActivity);

     info = findViewById(R.id.terv);

     new Thread(new Te()).start();

  }

  private class Te implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {                 
                final String message = (String) soc.objectInputStream.readObject();
                if (message != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            info.append(message);
                        }
                    });
                }
        } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
         }
   }
 }
 }

soc.java

public class soc  {

public static Socket clientSocket = null;
public static ObjectInputStream objectInputStream = null;
public static ObjectOutputStream objectOutputStream = null;

public static void socketException() {
    if (soc.clientSocket != null) {
        try {
            soc.clientSocket.close();
            soc.objectOutputStream.close();
            soc.clientSocket = null;
        } catch(Exception e2) {
            e2.printStackTrace();
        }
    }
}

}

Server.java

public class Server {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static OutputStream outputStream;
private static ObjectInputStream objectInputStream;
private static ObjectOutputStream objectOutputStream;
private static Activity activity;

public Server(Activity activity) {
    this.activity = activity;
}

public void startServer(int port) {
    try {
        serverSocket = new ServerSocket(port);
    } catch(Exception e) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, "Unable to start server", Toast.LENGTH_LONG).show();
            }
        });
        e.printStackTrace();
        return;
    }
    try {
        clientSocket = serverSocket.accept();
        inputStream = clientSocket.getInputStream();
        outputStream = clientSocket.getOutputStream();
        objectInputStream = new ObjectInputStream(inputStream);
        objectOutputStream = new ObjectOutputStream(outputStream);    
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void closeServer() {
    try {
        if (serverSocket != null) {
            serverSocket.close();
        }
        if (clientSocket != null) {
            clientSocket.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
        if (objectOutputStream != null) {
            objectOutputStream.close();
        }
        if (objectInputStream != null) {
            objectInputStream.close();
        }
    } catch(Exception e) {
        System.out.println(e);
    }
}

public static void sendMessageToServer(long message) {
    if (clientSocket != null) {
        try {
            objectOutputStream.writeObject(message);
            objectOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
            socketException();
        }
    }
}

private static void socketException() {
    Toast.makeText(activity, "Connection Closed", Toast.LENGTH_LONG).show();
    if (clientSocket != null) {
        try {
            clientSocket.close();
            objectOutputStream.close();
            clientSocket = null;
        } catch(Exception e2) {
            e2.printStackTrace();
        }
    }
}
}

The TextView works smoothly and shows the message when opening the SecondActivity for the first time.

But for the second time, when I go back to MainActivity and open the SecondActivity, the TextView doesn't work.

My app is fully based on this project.

I changed this project's MainActivity to soc.java class.

How can I solve this?

varunon9 commented 3 years ago

What message do you see for the first time? Any screenshot?

ghost commented 3 years ago

message received first time smoothly .

screen shot

pic3

Second time . when I send message from server it comes blank screen.

pic2

ghost commented 3 years ago

Can I use this in server.java ?

private static ServerSocket serverSocket;
public static Socket clientSocket;
private static InputStream inputStream;
private static OutputStream outputStream;
public static ObjectInputStream objectInputStream;
public static ObjectOutputStream objectOutputStream;
private static Activity activity;

private static Server instance = null;

private Server(Activity activity) {
    this.activity = activity;
}

public static Server getInstance(Activity activity){
    this.activity = activity;
    if (instance==null) instance = new Server(activity);
    return instance;
}

In my SecondActivity

Server.getInstance(activity).objectInputStream.readObject();

Will it work ?

varunon9 commented 3 years ago

Hi @parasuraam , It's hard to debug by just seeing some files. Can you share your project link (Github) if that is public?

ghost commented 3 years ago

My project : https://github.com/parasuraam/remote-pc

what I did wrong in this code ?

How can I solve this ?

ghost commented 3 years ago

When I go second time to SecondActivity it comes like this

W/System.err: java.io.StreamCorruptedException: invalid type code: 00
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1440)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
        at com.example.abcdefgh.SecondActivity$Te.run(SecondActivity.java:62)
        at java.lang.Thread.run(Thread.java:919)

My project : https://github.com/parasuraam/remote-pc

ghost commented 3 years ago

When I go first time to SecondActivity it comes like this

I/ViewRootImpl@5858aa9[SecondActivity]: ViewPostIme pointer 0
I/ViewRootImpl@5858aa9[SecondActivity]: ViewPostIme pointer 1
I/System.out: , STRING
D/InputMethodManager: prepareNavigationBarInfo() DecorView@d9b727a[SecondActivity]
   getNavigationBarColor() -1
V/InputMethodManager: Starting input: tba=com.example.abcdefgh ic=null mNaviBarColor -1 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: 'ClientS', fd=191
    Input channel destroyed: 'ClientS', fd=194
D/DecorView: semSetRoundedCorners: 5
I/System.out: first message from server

When I go second time to SecondActivity it comes like this

I/ViewRootImpl@bc8efbc[MainActivity]: ViewPostIme pointer 0
I/ViewRootImpl@bc8efbc[MainActivity]: ViewPostIme pointer 1
I/DecorView: [INFO] isPopOver=false, config=true
  updateCaptionType >> DecorView@da7b9ef[], isFloating=false, isApplication=true, hasWindowDecorCaption=false, hasWindowControllerCallback=true
D/DecorView: setCaptionType = 0, this = DecorView@da7b9ef[]
D/ScrollView: initGoToTop
D/InputTransport: Input channel constructed: 'b477eea', fd=191
I/ViewRootImpl@2476be2[SecondActivity]: setView = com.android.internal.policy.DecorView@da7b9ef TM=true
D/ViewRootImpl@2476be2[SecondActivity]: controlInsetsForCompatibility: hideByFlags=0x1, showByFlags=0x0, flags=0x81810500, sysUiVis=0x0, matchParent=true, nonAttachedAppWindow=true
D/InsetsSourceConsumer: setRequestedVisible: visible=false, type=0, host=com.example.abcdefgh/com.example.abcdefgh.SecondActivity, from=android.view.InsetsSourceConsumer.hide:228 android.view.InsetsController.collectSourceControls:1167 android.view.InsetsController.controlAnimationUnchecked:1044 android.view.InsetsController.applyAnimation:1412 android.view.InsetsController.hide:979 android.view.InsetsController.hide:962 android.view.ViewRootImpl.controlInsetsForCompatibility:2753 android.view.ViewRootImpl.performTraversals:3210 android.view.ViewRootImpl.doTraversal:2519 android.view.ViewRootImpl$TraversalRunnable.run:9775 
I/ViewRootImpl@2476be2[SecondActivity]: Relayout returned: old=(0,0,2340,1080) new=(83,0,2340,1080) req=(2340,1080)0 dur=9 res=0x7 s={true 540347759936} ch=true fn=-1
D/ScrollView:  onsize change changed 
I/ViewRootImpl@2476be2[SecondActivity]: [DP] dp(1) 1 android.view.ViewRootImpl.reportNextDraw:10761 android.view.ViewRootImpl.performTraversals:3733 android.view.ViewRootImpl.doTraversal:2519 
[DP] pd() Asnyc report
I/ViewRootImpl@2476be2[SecondActivity]: [DP] pdf(0) 1 android.view.ViewRootImpl.lambda$performDraw$1$ViewRootImpl:4554 android.view.-$$Lambda$ViewRootImpl$DJd0VUYJgsebcnSohO6h8zc_ONI.run:6 android.os.Handler.handleCallback:938 
[DP] rdf()
I/ViewRootImpl@bc8efbc[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
I/SurfaceControl: release : mNativeObject = 535515068896 - Surface(name=Surface(name=43092b8 InputMethod)/@0xc7af3f6 - animation-leash)/@0xef338ac
I/SurfaceControl: nativeRelease nativeObject s[535515068896]
  nativeRelease nativeObject e[535515068896]
I/SurfaceControl: release : mNativeObject = 535515093536 - Surface(name=Surface(name=c5c57dc NavigationBar0)/@0x5b3d07b - animation-leash)/@0xaf2a763
nativeRelease nativeObject s[535515093536]
nativeRelease nativeObject e[535515093536]
I/SurfaceControl: release : mNativeObject = 535515098352 - Surface(name=Surface(name=1e93f79 StatusBar)/@0x6fbc3ac - animation-leash)/@0x1e39f60
nativeRelease nativeObject s[535515098352]
nativeRelease nativeObject e[535515098352]
D/SurfaceControl: hide : mNativeObject = 536052178432 - sc.mNativeObject = 535515079424 - Surface(name=Surface(name=1e93f79 StatusBar)/@0x6fbc3ac - animation-leash)/@0xcba4c19
nativeSetFlags Done : Surface(name=Surface(name=1e93f79 StatusBar)/@0x6fbc3ac - animation-leash)/@0xcba4c19
V/ViewRootImpl@2476be2[SecondActivity]: updateAppliedLetterboxDirection, direction=1, Caller=android.view.ViewRootImpl.handleDispatchLetterboxDirectionChanged:11918
D/DecorView: semSetRoundedCorners: 5
I/ViewRootImpl@2476be2[SecondActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@da7b9ef[SecondActivity]
getNavigationBarColor() -1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@da7b9ef[SecondActivity]
getNavigationBarColor() -1
V/InputMethodManager: Starting input: tba=com.example.abcdefgh ic=null mNaviBarColor -1 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: 'ClientS', fd=198
Input channel destroyed: 'ClientS', fd=192
I/ViewRootImpl@bc8efbc[MainActivity]: stopped(true) old=false
I/SurfaceControl: release : mNativeObject = 535515081664 - Surface(name=com.example.abcdefgh/com.example.abcdefgh.SecondActivity$_14505)/@0x7de56bf
nativeRelease nativeObject s[535515081664]
I/SurfaceControl: nativeRelease nativeObject e[535515081664]
W/libEGL: EGLNativeWindowType 0x7dcf3bf510 disconnect failed
I/SurfaceControl: release : mNativeObject = 535515096224 - Surface(name=Surface(name=43092b8 InputMethod)/@0xc7af3f6 - animation-leash)/@0xa2ec0ea
nativeRelease nativeObject s[535515096224]
nativeRelease nativeObject e[535515096224]
I/ViewRootImpl@bc8efbc[MainActivity]: Relayout returned: old=(83,0,2340,1080) new=(83,0,2340,1080) req=(2257,1080)8 dur=6 res=0x5 s={false 0} ch=false fn=-1
D/SurfaceControl: hide : mNativeObject = 536052702736 - sc.mNativeObject = 535515091520 - Surface(name=Surface(name=43092b8 InputMethod)/@0xc7af3f6 - animation-leash)/@0x3ab9fdb
nativeSetFlags Done : Surface(name=Surface(name=43092b8 InputMethod)/@0xc7af3f6 - animation-leash)/@0x3ab9fdb
I/SurfaceControl: release : mNativeObject = 535515100704 - Surface(name=Surface(name=c5c57dc NavigationBar0)/@0x5b3d07b - animation-leash)/@0x929fa78
nativeRelease nativeObject s[535515100704]
I/SurfaceControl: nativeRelease nativeObject e[535515100704]
release : mNativeObject = 535515079424 - Surface(name=Surface(name=1e93f79 StatusBar)/@0x6fbc3ac - animation-leash)/@0xcba4c19
nativeRelease nativeObject s[535515079424]
nativeRelease nativeObject e[535515079424]
V/ViewRootImpl@bc8efbc[MainActivity]: updateAppliedLetterboxDirection, direction=0, Caller=android.view.ViewRootImpl.handleDispatchLetterboxDirectionChanged:11918
W/System.err: java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1440)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
    at com.example.abcdefgh.SecondActivity$Te.run(SecondActivity.java:62)
    at java.lang.Thread.run(Thread.java:919)
W/System.err: java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1440)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
    at com.example.abcdefgh.SecondActivity$Te.run(SecondActivity.java:62)
    at java.lang.Thread.run(Thread.java:919)
 W/System.err: java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1440)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
    at com.example.abcdefgh.SecondActivity$Te.run(SecondActivity.java:62)
    at java.lang.Thread.run(Thread.java:919)