Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have three activities A, B, C. A is my menu, upon button click it starts Activity B, in activity B, based on the outcome of an if statement Activity C is started.

The problem is C runs, the processes can be see in the logcat but doesn't open. B continues to play the alarm and vibrate and then the phone stalls and the alarm and vibrate continue. I have the code for the if statement above and the code for the openMaps() function as well, this is called based on the outcome of the if, in which I try to open Activity C and close B

EDIT

I have changed my code somewhat, ok, I have an idea what the problem is, I just don't know how to solve it. I have a thread, inside that thread is an infinate while loop, inside this infinite while loop I check the distance between two coordinates (i want them constantly updating).

The problem is, variables I have declared globally are initialized within the loop so I have to run my if statement within that, to access those...I tried using a boolean 'test' that if it's true execute the if statement then set test to false but that doesn't work I'm afraid.
Thread dist = new Thread(new Runnable() {
            public void run() {

                while (true) {

                    child = new Location("point A");

                    child.setLatitude(Clatitude);
                    child.setLongitude(Clongitude);

                    parent = new Location("point B");

                    parent.setLatitude(Platitude);
                    parent.setLongitude(Plongitude);

                    distance = child.distanceTo(parent);

                    // DisRound = Math.round(distance * 100.0) / 100.0;
                    DisRound = 550;

                    dynamicActivation = DisRound * weight;

                    try {
                        Thread.sleep(1000);

                        if (test)
                        {
                            if(dynamicActivation > threshold)
                            {
                                finish();
                                new Timer().schedule(new MapsTimer(), 5000);
                                test = false;
                            }
                            Log.d("ACTIVATION", Boolean.toString(test));
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        dist.start();

Here is the code for my MapsTimer that i'm calling in the if statement

private class MapsTimer extends TimerTask {
		@Override
		public void run() {
			runOnUiThread(new Runnable() {

				public void run() {
					int duration = Toast.LENGTH_SHORT;
					CharSequence text = "YOUR CHILD HAS TRAVELLED OUTSIDE PROXIMITY";
					Context context = getApplicationContext();
					proximityAlert = Toast.makeText(context, text, duration);
					proximityAlert.show();
					alarmSound.start();									
					
					openMaps();
				}
			});
		}

So, distance, DisRound and dynamicActivation are all global but initialized in the infinite loop, if I can access these outside this loop, I'm sure I can stop the problem. Anyone have any thoughts?


Regards,
Gary
Posted
Updated 23-Apr-13 8:48am
v5

1 solution

Hi,

I managed to get it sorted myself in case anyone might stumble across the same problem. I just inserted a break statement after I had done what I needed as it is obsolete after that.

Here is my code:

Thread dist = new Thread(new Runnable() {
            public void run() {

                while (true) {

                    child = new Location("point A");

                    child.setLatitude(Clatitude);
                    child.setLongitude(Clongitude);

                    parent = new Location("point B");

                    parent.setLatitude(Platitude);
                    parent.setLongitude(Plongitude);

                    distance = child.distanceTo(parent);

                    // DisRound = Math.round(distance * 100.0) / 100.0;
                    DisRound = 550;

                    dynamicActivation = DisRound * weight;

                    try {
                        Thread.sleep(1000);

                            if(dynamicActivation > threshold)
                            {
                                proximityAlert.show();
                                alarmSound.start();
                                new Timer().schedule(new MapsTimer(), 5000);
                                break;
                            }
                            Log.d("ACTIVATION", Boolean.toString(test));

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                finish();
            }
        });
        dist.start();


Once I break out of the loop I then call finish();
 
Share this answer
 
Comments
Richard C Bishop 23-Apr-13 15:12pm    
Good job figuring it out yourself.
GaryDoo 23-Apr-13 16:54pm    
thanks! it nearly broke my brain though!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900