Thursday 23 December 2010

Custom Timer for Android OS (with Cross Thread Messaging to Handle the UI updates )

Code below shows MyTimer class which implements the Runnable interface in order to be run as an individual thread. This class also supports MyTimerListener to inform the classes which are registered to this class, onTick event.

MyTimerClass

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MyTimer implements Runnable {

long interval;
List<MyTimerListener> l = new ArrayList<MyTimerListener>();
@Override
public void run() {
while(true)
{
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<MyTimerListener> it = l.iterator();
while(it.hasNext())
{
((MyTimerListener)it.next()).onTick();
}
}

}
MyTimer(long Interval)
{
interval=Interval;
}
public void addListener(MyTimerListener mtl)
{
if(!l.contains(mtl))
l.add(mtl);
}
public void removeListener(MyTimerListener mtl)
{
if(l.contains(mtl))
l.remove(mtl);
}
public void start()
{
Thread t = new Thread(this);
t.start();
}

}



MyTimerListerner interface responsible for handling the onTick event.


public interface MyTimerListener {
void onTick();
}



Implementation of MyTimer


android.os.Handler;

public class AttachedClass implements MyTimerListener {

private MyTimer mt = new MyTimer(1);
private Handler mHandler = new Handler();// Main thread attaaches this handler itself while it creates the instance of AttachedClass instance
public void startAttachedClass()
{
mt.start();// start method creates a new thread and runs mt in it.
}
@Override
public void onTick() {
Runnable r = new Runnable() {
public void run() {
TextView1.setText("Text has setted");
}
};
mHandler.post(r);// onTick event called by new thread which has been created in mt.start(). but TextView1 belonges to the main thread. In order to make handled setText method by new thread, Handler's post method has used which requires a Runnable(method) which will be run in main thread.

}
}
}


You can leave your questions to the comment section. Than you..

Monday 20 December 2010

Android TranslateAnimation sucks!

I really searched a lot on getting animated object's current location on the screen during the translate animation but I just found an android forum complaint title on it. And an Android officer was saying that It is not available to get this info during the animation and it could be added in the newer versions of android sdk. But this sucks and an important lack if you are developing a game in 2D. Animations are important and there are just 3 event listeners in IAnimationListener which listens 3 events of the animation; Start, End, Repeat. What about the during? I want to gather the info about my object which has being effected. Therefore, there is just 1 way for me, I will develop my own animation class rather to use TranslateAnimation. I will share it from here shortly.

Wednesday 8 December 2010

Damn developer.android.com is not working

The is the second time in last 3 days I am trying to connect to the Android Developer website (developer.android.com) and it is not responding again. It is a massive shame to Google. I hated it damnn!! the the worst is because of the Android Developer website is so good, there is no alternative answers to Android problems in anonymous website in the internet.

Edit: It has been Turkish Telecom's fault some wrong google IPs are stille banned because of Youtube Ban in Turkey. Grghhhhhhh....