How to alert vibrate loop with 10 seconds on Blackberry? - blackberry

I have a screen. I want alert vibrate with 10th seconds, 20th seconds, 30th seconds....
How can I do that ?
Thanks for reading

Here's a simple static method that uses a Timer to accomplish this. You pass in the amount of times to vibrate and the period between vibrations. The phone will vibrate for 100ms every repeatPeriodSeconds for repeatCount times.
public static void repeatVibrate(final int repeatCount, int repeatPeriodSeconds)
{
TimerTask task = new TimerTask()
{
private int repeats = 0;
#Override
public void run()
{
++repeats;
Alert.startVibrate(100);
if(repeats >= repeatCount)
{
cancel();
}
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, repeatPeriodSeconds * 1000L);
}
Here's the documentation of the relevant classes: Alert, Timer, TimerTask

This helps you:
public class LoadingScreen extends MainScreen
{
Timer timer;
TimerTask timerTask;
public LoadingScreen()
{
setTitle("Loading Screen");
callTheTimer();
}
public void callTheTimer()
{
timer=new Timer();
timerTask=new TimerTask()
{
public void run()
{
Alert.startVibrate(500);
}
};
timer.scheduleAtFixedRate(timerTask, 0, 3000);//For 10 secs give 10000; I am testing this for every 3 secs;
}
}
Before push new screen or closing the current screen give timertask.cancel();

Related

Display a progress screen for BlackBerry app

How to display a progress screen when BlackBerry application is fetching data from remote server ?
I want an progress screen without animated gif without any button. (OK or Cancel )
public class ProgressDialog extends PopupScreen
{
public ProgressDialog(String waitString)
{
super(new VerticalFieldManager());
add(new LabelField(waitString,Field.FIELD_HCENTER));//add a string which u want to show for progressing
}
}
ProgressDialog progress = new ProgressDialog("Please Wait...");
//Add screen to UI
UiApplication.getUiApplication().pushScreen(progress);
//Remove screen from UI
progress.close();
ProgressBar progressBar = new ProgressBar("Waiting for location update....",50,1000);
progressBar.start(); // Start progress bar
progressBar.remove(); //remove progress bar
ProgressBar Thread
public class ProgressBar extends Thread {
Thread thd;
private int maximum, timeout;
private boolean useful = true;
private PopupScreen popup;
private int iterations = 0;
/**
* Object constructor
*
* #param title
* Text to display on popup area
* #param maximum
* Range / width of the gauge field of progress bar
* #param timeout
* Milliseconds to pause between updates to progress bar
* #see GaugeField
* #see Thread
* #see PopupScreen
*/
public ProgressBar(String title, int maximum, int timeout) {
this.maximum = maximum;
this.timeout = timeout;
VerticalFieldManager manager = new VerticalFieldManager();
popup = new PopupScreen(manager);
manager.add(new LabelField(title));
}
/**
* run() method for starting thread
*/
public void run() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(popup);
}
});
int iterations = 0;
while (useful) {
System.out.println("I m here in while runn "+ useful);
try {
Thread.sleep(timeout);
} catch (Exception e) {
}
if (++iterations > maximum)
{
iterations = 1;
useful = false;
UiApplication.getUiApplication().invokeLater( new Runnable()
{
public void run ()
{
_locationProvider.reset();
Dialog.alert("Location not found.");
}
} );
}
// gaugeField.setValue(iterations);
}
if (popup.isDisplayed()) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(popup);
}
});
}
}
/**
* Method to shutdown the thread and remove the popup screen
*
*/
public synchronized void remove() {
useful = false;
}
}

How to display clock count down whole the application on Blackberry?

I make and display an clock count down with this code
LabelField time;
long mille=0;
Timer timer=null;TimerTask task=null;
public Timerscreen() {
mille=1000*60*1;
time=new LabelField();
add(time);
timer=new Timer();
task=new TimerTask() {
public void run() {
synchronized (UiApplication.getEventLock()) {
if(mille!=0){
SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
time.setText(date.formatLocal(mille));
mille=mille-1000;
}else{
time.setText("00:00");
mille=1000*60*1;
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Time expaired");
}
});
}
}
}
};
timer.schedule(task,0, 1000);
And when I push a new screen , I want to this clock still display and count down.
How can I do that ?
It is not possible to add a single ui field or manager into two managers or screens.. every ui field or manager must have at most one parent (screen or manager).
So if you need a LabelField which will hold and show time on different screens, then you only need to implement some sort of listener which will listen for the time changes.. and for every changes you have to update the screen and the LabelField with the new value. You have already implemented a TimerTask which will provide you updated data.
[Edited - added later]
you can check the following codes, not tested but something like this will solve your problem...
class MyTimerUtil {
TimerListener listener = null;
public MyTimerUtil() {
}
public void setTimerListener(TimerListener listener) {
this.listener = listener;
}
public void startTimer() {
final int interval = 1000;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
// add your codes..
// notify others
if (listener != null) {
listener.timeChanged();
}
}
};
timer.schedule(task, 0, interval);
}
}
interface TimerListener {
public void timeChanged();
}
class ScreeA extends MainScreen implements TimerListener {
public void timeChanged() {
// add Codes here on time changed event
}
}
in the above snippet, you can implement TimerListener interface in any screen instance and can get update on every time changed event by the MyTimerUtil class. For that, you have to set an instance of ScreeA (which implements TimerListener) via setTimerListener() of the MyTimerUtil class.
Also need to start the timer by calling startTimer() method.

how to check timer is running on blackberry

I've add timer to display images in my app.
is there any way to check the timer is running or not.?
after checking , the timer should be cancel using timer.cancel() method.
Pls hlp me.
Blackberry's Timer is very cheesy - it's just like a Runnable with Thread.sleep() inside. Very commonly for Blackberry, it contains lot of crap you don't actually need and doesn't contain things you do need.
I would dump the Timer and make a class specially for my needs:
abstract public class MyTimer extends Thread {
private final Object waitobj = new Object();
private volatile boolean running;
private volatile boolean canceled;
private final long due;
public MyTimer setDelay(long delay) {
long cur = System.currentTimeMillis();
due = cur + delay;
return this;
}
public MyTimer setAlarmTime(long dueTimeMillis) {
due = dueTimeMillis;
return this;
}
synchronized void setIsRunning(boolean running) {
this.running = running;
}
synchronized public boolean isRunning() {
return running;
}
synchronized public void cancel() {
synchronized (waitobj) {
canceled = true;
waitobj.notify();
}
}
public void run() {
setIsRunning(true);
long cur = System.currentTimeMillis();
long sleep = due - cur;
while (sleep > 0) {
synchronized (waitobj) {
waitobj.wait(sleep);
}
if (isCanceled()) return;
cur = System.currentTimeMillis();
sleep = due - cur;
}
alarm();
setIsRunning(false);
}
private boolean isCanceled() {
return canceled;
}
abstract void alarm();
}
Then I would invoke it like this:
timer = new MyTimer() {
void alarm() {
// do cool things
}
};
timer.setDelay(10000).start();
If I need to cancel it I would do it like this:
if (timer.isRunning()) {
timer.cancel();
}
or simply
timer.cancel();
PS: Note volatile and synchronized things in MyTimer class.
You can manage this yourself by recording the timers unique integer and use it later to cancel. I find a useful place to set/cancel this is in the onVisibilityChanged(boolean) override. I'm assuming here your timed images are for animation.
// start
if (renderLoop==-1) renderLoop = UiApplication.getUiApplication().invokeLater( this, 50, true );
// stop
if (renderLoop!=-1)
{
UiApplication.getUiApplication().cancelInvokeLater( renderLoop );
renderLoop = -1;
}
//assumes your screen implements Runnable
public void run() {
// do something cool
}
Assuming that you're programming in Java with Swing, you have a method called isRunning() within the Timer Class.
http://download.oracle.com/javase/7/docs/api/javax/swing/Timer.html#isRunning%28%29
Regards

Blackberry : How to change image periodically in my screen?

I want to change the image in my application screen periodically.
how can i implement it ?
is there any timer control(like in iPhone) for BlackBerry ?
waiting for your suggessions...
thanks in advance...
try this.
try {
timer = new Timer();
timer.scheduleAtFixedRate(new ImageTask(), 0, 5000);
} catch (Exception e) {
// do nothing
}
private class ImageTask extends TimerTask {
public void run() {
System.out.println("Test Printing..");
}
}

BlackBerry - Simulate a KeyPress event

I have a BlackBerry application that needs to take pictures from the camera and send them to a server. In order to do this i invoke the native camera application and listen to the filesystem. Once an image is captured and saved as a new jpeg file i get notified, resume foreground control and go about my business. The problem starts occurring after the first time this cycle is completed because now when i decide to call the camera application again it is already opened, and now the user is seeing a thumbnail of the last picture that was taken and several buttons allowing him to manipulate/manage it. naturally what i want the user to see is a preview of what the camera is "seeing" before he snaps another photo as he did before.
I have thought of various ways to solve this including killing the camera app each time (I understand this cannot be done programatically?), sending CameraArguments when invoking the app (which appears to be useless), and now i was thinking a solution could be as simple generating a "Back" key event before switching back to my app which would theoretically dismiss the annoying edit screen. Could this really be done? and if not is there any other possible solution you may think of?
A kind of hack...
start Camera App
in TimerTask check if Camera App started and if it need to be closed (some flag)
if yes, invoke it(so it will became active) and push ESC keypress event injection to close it
Take a look at this:
class Scr extends MainScreen {
boolean killCameraApp = false;
final String mCameraModuleName = "net_rim_bb_camera";
final CameraArguments args = new CameraArguments();
public Scr() {
super();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (isCameraRunning() && killCameraApp) {
getApplication().invokeAndWait(callCamera);
getApplication().invokeAndWait(killCamera);
}
}
}, 0, 100);
}
Runnable callCamera = new Runnable() {
public void run() {
callCamera();
}
};
Runnable killCamera = new Runnable() {
public void run() {
injectKey(Characters.ESCAPE);
killCameraApp = false;
}
};
private boolean isCameraRunning() {
boolean result = false;
ApplicationManager appMan =
ApplicationManager.getApplicationManager();
ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
for (int i = 0; i < appDes.length; i++) {
result = mCameraModuleName.equalsIgnoreCase(appDes[i]
.getModuleName());
if (result)
break;
}
return result;
}
private void callCamera() {
Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA,
new CameraArguments());
}
private void injectKey(char key) {
KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
inject.post();
}
protected void makeMenu(Menu menu, int instance) {
menu.add(new MenuItem("start camera", 0, 0) {
public void run() {
callCamera();
killCameraApp = false;
}
});
menu.add(new MenuItem("kill app", 0, 0) {
public void run() {
killCameraApp = true;
}
});
super.makeMenu(menu, instance);
}
}
EDIT: Don't forget to set permissions for device release:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection

Resources