0

while trying to "develop" a code to drive a robot using an android device, I've been stranded on a stupid issue and after looking to solve it for weeks, I will finally ask you for help. I'm totally new to android and also to java. I'm basically using the bluetoothchat example just changing the main layout drawing a remote with 4 arrows. In order to use those arrows, I've implemented a function which should send a Byte every 300ms as long as the given imageButton is pressed. see following code and don't be too angry if it is a mess (I'm beginner):

private void OnTouch(ImageButton IB , final String str){

    IB.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, final MotionEvent ev){

             Thread t = new Thread() {

                public void run() {
                try{
                    if(ev.getAction()== MotionEvent.ACTION_DOWN){
                        a.set(false);
                        Log.e(TAG, "a set to false");
                    }
                    if(ev.getAction()== MotionEvent.ACTION_MOVE){
                        a.compareAndSet(true, false);
                    }
                    if(ev.getAction() == MotionEvent.ACTION_UP){
                        Log.e(TAG, "actionUp");
                        a.set(true);
                        Log.e(TAG, "a set to true");
                    }
                }
                finally{}
                }
            };

            t.start();
            while(!a.get()){

                    sendMessage(str);

                    Log.e(TAG, "message sent");
                    try {
                        Thread.sleep(300);
                        Log.e(TAG, "dodo");
                    } catch (InterruptedException e) {
                        Log.e(TAG, "error thread.sleep");
                        e.printStackTrace();
                    }
                }

            if(a.get())
            {
                mOutStringBuffer.setLength(0);
                Log.e(TAG, "StringBuffer=0");
                sendMessage(Stop);  
                Log.e(TAG, "S sent");
            }

            t.stop();
            Log.e(TAG, "t.stop and return");
            return a.get();
        }
    });
    }

The atomicBoolean a is set like that at the begining of the class : private AtomicBoolean a = new AtomicBoolean(true);

My issue is the following: when I press a button and hold it the logcat shows "StringBuffer=0" and then "a set to false" "S sent" "t.stop and return" and then nothing for a while (about 12 seconds) and then it begins to loop in "message sent" "dodo" without ending even if I lift my finger. If I don't let my finger too long (simple click), I obtain: "stringBuffer=0" "S sent" "a set to false" "t.stop and return" "actionUp" "a set to true" "message sent" "dodo" "StringBuffer=0" "S sent" "t.stop and return"

I'm really lost and don't understand what's going on with this function.

If it helps, I'm debugging on a samsung galaxy tab (GT-P1000) firmware version 2.2.1

Hope someone can help me debugging it and explain me my issues...

  • You should not call `t.stop()` - the method [is clearly marked as deprecated](http://developer.android.com/reference/java/lang/Thread.html#stop%28java.lang.Throwable%29), for good reasons. – assylias Aug 21 '12 at 12:51

1 Answers1

0

As I understand it, you're sleeping and looping in the UI thread. You're not meant to do that - it stops the UI from reacting to further events. Instead, you should operate in a reactive way - if you want something to happen "in 300 milli-seconds" then set a timer to do so, and cancel it if some other appropriate action occurs before then.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • could you help me to implement this method? please, I don't think I'm able to because I tried something like that but never could access the variable I wanted... – user1614160 Aug 21 '12 at 12:53
  • @user1614160: It doesn't help that we don't know what "sendMessage" does... it sounds like *that* should happen on a non-UI thread, but everything else should be on the UI thread. – Jon Skeet Aug 21 '12 at 12:55
  • sorry, I didn't mention this function because as I said, I used the bluetoothchat example from the android sdk and this function is part of it and does work. it sends bytes using bluetooth basicaly and using a handler but I don't think it is the problem – user1614160 Aug 21 '12 at 13:05
  • @user1614160: But the threading *is* the problem. It certainly *looks* like all of this is happening in the wrong thread. – Jon Skeet Aug 21 '12 at 13:07
  • Ok, I can't unerstand how this could be usefull because I didn't touch this function and it works well in the bluetoothcat example. As I don't know which part of the code could interest u, could u please check it directly in the bluetoothchat example? Thanks for your fast responses – user1614160 Aug 21 '12 at 13:16
  • @user1614160: No, I don't have time for that. You should read up on: a) threading models in Android, particularly what you should do on which thread; b) using timers for repeated events. `Thread.sleep` is almost certainly the *wrong* approach, and you almost certainly shouldn't be creating a new thread each time the event fires, either. – Jon Skeet Aug 21 '12 at 13:20