Why "DISCOVERY_FINISHED" Message printed twice? - android

I want to Search Bluetooth Device Every three seconds.
so, I used Timer like this.
public void SearchingDevice() {
m_BTAdapter.startDiscovery();
m_timer = new Timer(true);
TimerTask timerTask = new TimerTask() {
public void run() {
m_BTAdapter.cancelDiscovery();
m_BTAdapter.startDiscovery();
}
};
m_timer.schedule(timerTask, 3000, 3000);
}
By the way, "android.bluetooth.adapter.action.DISCOVERY_FINISHED" Message
always printed twice..... why this message printed twice??
I used cancelDiscovery() only once...
please someone help me..!!
Thanks.

You should be careful with timer task. Maybe the problem is that you are not canceling task with activityLifecycle and each time you are creating a new one.
try:
#Override
protected void onPause() {
super.onPause();
m_timer.cancel();
}
can you paste the piece of code where you call SearchingDevice() method?

Related

I cant make myHandler run

What I want to do is just a basic implementation of handler example. I have a TextView on the mainActivity, and once the page loads the handler is supposed to run and show the user value coming from SystemClock.uptimeMillis. But ıt doesn't work more than once. How can I make this code run?
public class MainActivity extends Activity {
long uptoMS=0L;
TextView tv;
Handler handler=new Handler();
long swaptime=0L;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
uptoMS=SystemClock.uptimeMillis();
tv.setText(String.valueOf(uptoMS));
handler.post(runner);
}
private Runnable runner=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
swaptime+=uptoMS;
tv.setTag(String.valueOf(swaptime));
handler.post(this);
}
};
}
See this below example
scheduler(){
TimerTask tasknew = new TimerSchedulePeriod();
Timer timer = new Timer();
// scheduling the task at interval
timer.schedule(tasknew,100, 100);
}
// this method performs the task
public void run() {
System.out.println("timer working");
}
timer = new Timer();
refreshTask = new TimerTask() {
#Override
public void run() {
swaptime+=uptoMS;
tv.setTag(String.valueOf(swaptime));
handler.post(this);
}
};
timer.schedule(refreshTask,
100, 100);
Well, there some problems with your code.
Firstly, you use setTag() instead of setText(), so the value will never update.
tv.setTag(String.valueOf(swaptime));
Secondly, you get uptoMS once in onCreate(), and then you use it in every "handler loop". I don't know what you try to achive but it's unlike that you want this.
Thirdly, you instantly repost your Runnable, so the main thread's message queue will be busy. You should give some break instead of instant reposting. For example you can wait 100 ms between the updates, so the TextView will be updated 10 times in every second.
handler.postDelayed(this, 100);
And finally, however others suggest you using Timer, just ignore them. Handler is the Android way to achieve tasks like this.

Android Timer is called twice

Hey guys
I'm trying to develop a little slide show in android. I've implemented the timer with a timertask and as expected the "run"-method is called everytime after the delay. But, however, when the delaytime has passed, the run method is called twice, so that the slide show slides two pictures instead of one.
Can anyone help me to solve this problem? Heres some code for better understandig what i've done.
public void startTimer() {
if(timer == null) {
timer = new Timer();
}
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
timerHandler.post(new Runnable() {
#Override
public void run() {
Log.d("Hier herein", "Timer abgelaufen");
}
});
}
}, 7000, 7000);
}
The startTimer()-Method is only called once, so I really don't understand, why the function is called twice.
Thanks in advance :D

Timer in android?

Can some body please help me in this.I am implementing a swiper application that reads the card details and returns a string.I want to implement a timer that will check whether this string is returned or not.Can somebody please tell me how to find whether a string contains a value after the oncreate method is called.I basically want to know how to check a string contains a value upto a certain time(20 seconds).If no value is retirned upto 20 seconds i will be showing a pop up.I am new to android programming any help would be appreciated.
Following is what i am trying to do in my oncreate method.
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
if(result.length()==0)
{
Log.e("TAG","No value received");
}
}
}
};
timer.schedule(task, 20000,1000);
I suggest using a Handler.
Handler h = new Handler();
h.postDelayed(new Runnable() {
void run() {
// your job to be done in delay seconds
}, delay);

How can I create timer tick in Android?

I have this method
public void GetSMS(){
//in this method I read SMS in my app inbox,
//If have new SMS create notification
}
for this I think create timer tick method and every 5 sec call GetSMS()
How can I create a correct method for that ?
Here is an example of Timer and Timer Task. Hope this helps.
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
// Do whatever you want
}
});
}
};
timer.schedule(timerTask, 1000); // 1000 = 1 second.
Maybe with a timer and a timertask?
See javadocs:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html
Yet receiving broadcasts is probably a more solid solution.
See: Android - SMS Broadcast receiver
Use Timer.scheduleAtFixedRate() as follow:
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
GetSMS();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 5000, 5000); // every 5 seconds.
I saw it by accident.. This is not the right way to do it..
You don't need to check if there is a sms that received. Android provide broadcast receiver to get notified when sms is income.
Here you go, you have the link here.. Copy paste and it will work great
http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
Hope that this make sense
Although the above timer methods are the correct way to use timers of the sort you are after, I quite like this little hack:
new CountDownTimer(Long.MAX_VALUE, 5000)
{
public void onTick(long millisUntilFinished)
{
// do something every 5 seconds...
}
public void onFinish()
{
// finish off when we're all dead !
}
}.start();
Long.MAX_VALUE has, according the Java docs, a (signed) value of 2^63-1, which is around 292471 millennia ! So starting up one of these countdown timers effectively lasts forever relatively speaking. Of course this depends on your interval time. If you want a timer every 1 second the timer would "only" last 58494 millenia, but we don't need to worry about that in the grander scheme of things.

Service Timer Notification

HI!
I want make service in OnCreate(), and every five minute, the service show notification..
can you show me about it??
thanks before :)
You can use the TimerTask class with the postDelayed method.
private TimerTask mTask = new TimerTask() {
#Override
public void run() {
//Whatever you want
postDelayed(this, REPEAT_INTERVAL); // rinse and repeat...
}
};
And in your OnCreate launching the TimerTask for first time:
postDelayed(mTask, INITIAL_DELAY);
You can find some information in this android article
http://developer.android.com/resources/articles/timed-ui-updates.html

Categories

Resources