Reputation: 141
I'm trying to get GPS location every one second once the app starts. Currently I have my code looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editLocation = (EditText) findViewById(R.id.editTextLocation);
btnGetLocation = (Button) findViewById(R.id.btnLocation);
btnGetLocation.setOnClickListener(this);
gps = new GPSTracker(GetCurrentLocation.this);
getLocation();
}
public void getLocation() {
Calendar c = Calendar.getInstance();
final int seconds = c.get(Calendar.SECOND);
Runnable runnable = new Runnable() {
public void run() {
try {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
}
String body = latitude + "\t" + longitude + "\t" + seconds;
editLocation.setText(body);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
};
new Thread(runnable).start();
}
However, it only runs once (when the app starts) and it never run the second time. I'm using the seconds to check if it runs periodically.
UPDATE: This is what I got from logcat
04-15 11:11:20.137: E/AndroidRuntime(16160): FATAL EXCEPTION: Thread-3450 04-15 11:11:20.137: E/AndroidRuntime(16160): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6818) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1112) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4484) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.view.View.invalidate(View.java:11353) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.invalidateRegion(TextView.java:5378) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.invalidateCursor(TextView.java:5321) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.spanChange(TextView.java:8581) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:10686) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.Selection.setSelection(Selection.java:116) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.Selection.setSelection(Selection.java:127) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.setText(TextView.java:4434) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.setText(TextView.java:4283) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.EditText.setText(EditText.java:108) 04-15 11:11:20.137: E/AndroidRuntime(16160): at android.widget.TextView.setText(TextView.java:4258) 04-15 11:11:20.137: E/AndroidRuntime(16160): at com.yc.test.GetCurrentLocation$1.run(GetCurrentLocation.java:60) 04-15 11:11:20.137: E/AndroidRuntime(16160): at java.lang.Thread.run(Thread.java:841)
and the modified code:
Thread checker;
public void getLocation() {
Runnable runnable = new Runnable() {
public void run() {
Calendar c = Calendar.getInstance();
while (true) {
int seconds = c.get(Calendar.SECOND);
try {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
}
String body = latitude + ", \t" + longitude + "\t"
+ seconds;
editLocation.setText(body);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
};
checker = new Thread(runnable);
checker.start();
}
Upvotes: 0
Views: 165
Reputation: 6156
So, I haven't got enough rep to comment on Bolics answer :( Look at this answer like a comment please.
"I tried that and the entire app crashes" - Malluce This is, because when you're looping forever in onCreate(), the app doesn't do anything else. Thus, you need to create another Thread, as Farnabaz suggested. Maybe use AsyncTask if you'd like to show a progressDialog in the main activity. If you don't need to 'communicate' with the main activity from within that second Thread, you can simply do what Farnabaz suggested.
(I wrote this to explain your errors and the answer. If this is frowned upon because I haven't any "new" facts, please tell me:) )
Upvotes: 0
Reputation: 4066
I Think this is what you want
Thread checker ;
public void getLocation() {
Runnable runnable = new Runnable() {
public void run() {
Calendar c = Calendar.getInstance();
while(true){ // loop forever and check GPS location
int seconds = c.get(Calendar.SECOND);
try {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
}
String body = latitude + "\t" + longitude + "\t" + seconds;
editLocation.setText(body);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// end while
};
};
checker = new Thread(runnable).start();
}
call checker.cancel();
when you want to stop checking
Upvotes: 1
Reputation: 745
You are only starting a thread which is doing the code once. You need a loop:
while(true){
//do your stuff
sleep(1000) // to wait the second
}
Upvotes: 1