Showing posts with label Listener. Show all posts
Showing posts with label Listener. Show all posts

Monday, March 9, 2009

Support : setMediaTime does not work for AMR files

1 comments

When using the Player method to seek the media time in an Adaptive Multi-Rate (AMR) encoded file, seeking past the first 58 KB of the file does not work. Seeking is possible within the first 58 KB of the AMR file, provided the file size is known, and setMediaTime is called after the PlayerListener.STARTED event is received.

To work around this limitation, implement and call a custom DataSource method to handle the seeking separately.

See DB-00612 for instructions on creating a custom DataSource and SourceStream.

The SourceStream implementation should contain a special operation to handle the AMR seeking. The following is an example for 12.2kbps AMR files, which is the encoding standard used for files recorded on the BlackBerry device. AMR files are broken into 20 millisecond frames, and 12.2kbps files have 32bytes per frame. Each AMR file also has a 6 byte header.

public void amrSeek(long millis) {
int frame = (int) millis/20;
readLocation = (framenumber * 32) + 6;
}

The SourceStream read method should use the readLocation variable to define where reads to the media stream will start because it is the beginning of the frame that has been seeked.

Monday, March 2, 2009

Code : Allow a background listener to detect and update a GUI application

1 comments

When alternate entry points are used, an application may consists of both a background listening thread and a foreground graphical user interface (GUI) application. In many cases, it is desired that the background listening thread detects that the GUI portion of an application is running, and updates the screen to alert the user of an event.

The following example makes use of two entry points. The FolderListener is set to auto-start through an alternate entry point, and the GUI portion of the application is started when the user clicks on the application icon on their BlackBerry device.

Note: This example uses a FolderListener, but this principle can be applied to any background listening thread.

Create an alternate entry point for an application:

  1. Create a project in the BlackBerry Java Development Environment (JDE).
  2. Right-click the project and click Properties.
  3. Click the Application tab.
  4. In the Project type drop-down list, click Alternate CLDC Application Entry Point.
  5. In the Alternate entry point for drop-down list, select the project that you wish to auto-start.
  6. In the Arguments passed to field, type init (This can be any string, init is used in the sample below).
  7. Select the System module option.
  8. Select the Auto-run on startup option.
  9. Click OK.

/**
* FolderListenerGUI.java
*/
package com.samples.folderListener;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
public class FolderListenerGUI extends UiApplication
{
private MainScreen mainScreen;
private static RichTextField newMailNotifier;
public static void main(String[] args)
{
boolean startup = false;
//Check parameters to see if the application was entered
//through the alternate application entry point.
for (int i=0; i<args.length; ++i)
{
if (args[i].startsWith("init"))
{
startup = true;
}
}
if (startup)
{
//Entered through the alternate application entry point.
//Enable folderlistener on startup.
FolderListenerApp.waitForSingleton();
} else
{
//Entered by selecting the application icon on the ribbon.
//Start the FolderListenerGUI.
FolderListenerGUI app = new FolderListenerGUI();
app.enterEventDispatcher();
}
}
public FolderListenerGUI()
{
//Pass a reference to the FolderListenerGUI
//to the FolderListenerApp.
FolderListenerApp.waitForSingleton().
setFolderListenerGUI(this);
//Initialize mainScreen.
mainScreen = new MainScreen();
//Initialize the fields.
newMailNotifier = new RichTextField("No mail yet...",
RichTextField.NON_FOCUSABLE);
LabelField title = new LabelField("Folder Listener GUI",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
mainScreen.setTitle(title);
mainScreen.add(newMailNotifier);
//Display the main screen
pushScreen(mainScreen);
}
//Update the RichTextField with a new mail message.
public static void NewMailArrived()
{
newMailNotifier.setText("New mail arrived!");
}
//Remove the FolderListenerGUI instance from
//FolderListenerApp on exit.
protected void onExit()
{
//Remove the reference to FolderListenerGUI
//from FolderListenerApp.
FolderListenerApp.waitForSingleton().unsetFolderListenerGUI();
}
}
/**
* FolderListenerApp.java
*
*/
package com.samples.folderListener;
import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.system.*;
import net.rim.blackberry.api.mail.*;
public final class FolderListenerApp implements FolderListener {
private FolderListenerGUI folderListenerGUI = null;
public static final long RTSID_MY_APP = 0x7451402f595f81a5L;
public FolderListenerApp()
{
try
{
//Get the store from the default instance.
Store store = Session.getDefaultInstance().getStore();
//Add the folder listener to the store.
store.addFolderListener(this);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
//Returns an instance of the running FolderListenerApp.
public static FolderListenerApp waitForSingleton()
{
//Ensure this is a singleton instance.
//Open the RuntimeStore.
RuntimeStore store = RuntimeStore.getRuntimeStore();
//Obtain the reference of FolderListenerApp.
Object obj = store.get(RTSID_MY_APP);
//If obj is null, there is no current reference
//to FolderListenerApp. Start a new instance
// of FolderListenerApp if one is not running.
if (obj == null)
{
//Store a reference to this instance in the RuntimeStore.
store.put(RTSID_MY_APP, new FolderListenerApp());
return (FolderListenerApp)store.get(RTSID_MY_APP);
} else
{
return (FolderListenerApp)obj;
}
}
//Called when a new message is created.
public void messagesAdded(FolderEvent e)
{
//Get the message that fired the folder event.
Message orginalMessage = e.getMessage();
//Get the folder the message is in.
Folder messageFolder = orginalMessage.getFolder();
//Is the new message in the Inbox?
if (messageFolder.getType() == Folder.INBOX)
{
//Check if the FolderListenerGUI is running.
if (folderListenerGUI != null)
{
//Grab the lock for the running FolderListenerGUI.
folderListenerGUI.invokeAndWait(new Runnable()
{
public void run()
{
//Call the NewMailArrived method to update the
//screen with a notification.
folderListenerGUI.NewMailArrived();
}
});
}
}
}
//Passes in reference to the FolderListenerGUI that is running.
public void setFolderListenerGUI(FolderListenerGUI folderGUI)
{
folderListenerGUI = folderGUI;
}
//Set the reference to the FolderListenerGUI to null
//(when it is shut down).
public void unsetFolderListenerGUI()
{
folderListenerGUI = null;
}
//Not used in this sample.
public void messagesRemoved(FolderEvent e)
{
}
}

Saturday, February 21, 2009

How to - Capture power change events

0 comments

Your application can be notified when power change events take place on the BlackBerry device. This could be used to halt certain features when the battery power is low, and resume them when it is back to normal levels. Implementing the SystemListener interface (net.rim.device.api.system.SystemListener) can perform these operations. Events such as the BlackBerry powering on, powering off, and changes in the battery state can be captured. The following sample application shows how to implement the SystemListener interface:

import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
public class PowerChangeEvent
extends Application implements SystemListener
{
public static PowerChangeEvent theApp;
public static void main(String args[])
{
theApp = new PowerChangeEvent();
theApp.enterEventDispatcher();
}
public PowerChangeEvent()
{
//Register this instance with the system listener.
Application.getApplication().addSystemListener(this);
System.out.println("PowerChangeEvent: PowerChangeEvent has started!");
}
//Invoked when the user is putting the device into a power off state.
public void powerOff()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering off.");
}
//Invoked when the device has left the power off state.
public void powerUp()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering up.");
}
//Invoked when the internal battery voltage falls below a critical level.
public void batteryLow()
{
System.out.println("PowerChangeEvent: The battery is getting low!");
}
//Invoked when the internal battery voltage has returned to normal.
public void batteryGood()
{
System.out.println("PowerChangeEvent: The battery level is now good!");
}
//Invoked when the internal battery state has changed.
//Not used in this sample.
public void batteryStatusChange(int status)
{
}
}

How To - Display a PopupScreen from a SendListener

0 comments

The SendListener interface can be triggered by a foreground application that displays a GUI (such as the Messages application or a third-party application) or by a background application or process that does not contain a GUI (such as a third-party application or when appointment invitations are sent from the Calendar application). Each case requires a different approach to display a PopupScreen to the BlackBerry smartphone user, resulting in the SendListener detecting what type of application is triggering it and displaying the PopupScreen appropriately. Failing to do so can result in the PopupScreen not being shown to the BlackBerry smartphone user.

The following code example demonstrates how to detect the type of application triggering the SendListener and how to display a Dialog for each case.

public class MySendListener extends Application implements SendListener
{
public boolean sendMessage(Message msg)
{
Dialog myDialog = new Dialog(Dialog.D_OK,
"This is a Message from a SendListener",
Dialog.OK,Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
Dialog.GLOBAL_STATUS)
{
//Override inHolster to prevent the Dialog from being dismissed
//when a user holsters their BlackBerry. This can
//cause a deadlock situation as the Messages
//application tries to save a draft of the message
//while the SendListener is waiting for the user to
//dismiss the Dialog.
public void inHolster()
{
}
};
//Obtain the application triggering the SendListener.
Application currentApp = Application.getApplication();
//Detect if the application is a UiApplication (has a GUI).
if( currentApp instanceof UiApplication )
{
//The sendMessage method is being triggered from
//within a UiApplication.
//Display the dialog using is show method.
myDialog.show();
}
else
{
//The sendMessage method is being triggered from
// within an application (background application).
Ui.getUiEngine().pushGlobalScreen( myDialog, 1,
UiEngine.GLOBAL_MODAL );
}
return true;
}
}

Wednesday, February 18, 2009

Code : Implement the PhoneListener interface

2 comments

BlackBerry application programming interface (API) 4.0 introduced the ability to listen to the status of incoming and outgoing phone calls. To do so, an application must implement the PhoneListener (net.rim.blackberry.api.phone.PhoneListener) or AbstractPhoneListener class (net.rim.blackberry.api.phone.AbstractPhoneListener). The following code sample illustrates the implementation of the PhoneListener interface:


import net.rim.blackberry.api.phone.*;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.*;
public final class PhoneCallListenerSample extends Application
implements PhoneListener
{
static public void main(String[] args)
{
PhoneCallListenerSample plSample = new PhoneCallListenerSample();
plSample.enterEventDispatcher();
}
private PhoneCallListenerSample()
{
System.out.println("###PhoneCallListenerSample:
Registering listener...");
Phone.addPhoneListener(this);
System.out.println("###PhoneCallListenerSample:
Listener registered.");
}
private void showPhoneEvent(String reason, int callId)
{
Status.show("Phone event: " + reason + "\n" + "CallID: " + callId,
Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), 4000,
Status.GLOBAL_STATUS, true, false, 2);
System.out.println("###PhoneCallListenerSample: Phone event: " +
reason + " for callID: " + callId);
}
// A call has been added to a conference call
public void callAdded(int callId)
{
showPhoneEvent("Call Added", callId);
}
// User answered a call
public void callAnswered(int callId)
{
showPhoneEvent("Call Answered", callId);
}
// Conference call established
public void callConferenceCallEstablished(int callId)
{
showPhoneEvent("Conference Call Established", callId);
}
// Network indicates a connected event
public void callConnected(int callId)
{
showPhoneEvent("Call Connected", callId);
}
// Direct-connect call connected
public void callDirectConnectConnected(int callId)
{
showPhoneEvent("DirectConnect Connected", callId);
}
// Direct-connect call disconnected
public void callDirectConnectDisconnected(int callId)
{
showPhoneEvent("DirectConnect Disconnected", callId);
}
// Call disconnected
public void callDisconnected(int callId)
{
showPhoneEvent("Call Disconnected", callId);
}
// User ended call
public void callEndedByUser(int callId)
{
showPhoneEvent("Call Ended By User", callId);
}
// Call has been placed on "hold"
public void callHeld(int callId)
{
showPhoneEvent("Call Held", callId);
}
// New call has arrived
public void callIncoming(int callId)
{
showPhoneEvent("Call Incoming", callId);
}
// Outbound call initiated by the handheld
public void callInitiated(int callid)
{
showPhoneEvent("Call Initiated", callid);
}
// Call removed from a conference call
public void callRemoved(int callId)
{
showPhoneEvent("Call Removed", callId);
}
// Call taken off of "hold"
public void callResumed(int callId)
{
showPhoneEvent("Call Resumed", callId);
}
// Call is waiting
public void callWaiting(int callid)
{
showPhoneEvent("Call Waiting", callid);
}
// Conference call has been terminated
// (all members disconnected)
public void conferenceCallDisconnected(int callId)
{
showPhoneEvent("Conference Call Disconnected", callId);
}
// Call failed
public void callFailed(int callId, int reason)
{
String errMsg = "Call Failed due to ";
// determine reason
switch( reason )
{
case PhoneListener.CALL_ERROR_AUTHORIZATION_FAILURE:
showPhoneEvent(errMsg + "Authorization Error", callId);
break;
case PhoneListener.CALL_ERROR_CALL_REPLACED_BY_STK:
showPhoneEvent(errMsg + "Call Replaced By Stack", callId);
break;
case PhoneListener.CALL_ERROR_CONGESTION:
showPhoneEvent(errMsg + "Congestion", callId);
break;
case PhoneListener.CALL_ERROR_CONNECTION_DENIED_BY_NETWORK:
showPhoneEvent(errMsg + "Connection Denied", callId);
break;
case PhoneListener.CALL_ERROR_DUE_TO_FADING:
showPhoneEvent(errMsg + "Fading", callId);
break;
case PhoneListener.CALL_ERROR_EMERGENCY_CALLS_ONLY:
showPhoneEvent(errMsg + "Emergency Calls Only", callId);
break;
case PhoneListener.CALL_ERROR_FDN_MISMATCH:
showPhoneEvent(errMsg + "FDN Mismatch", callId);
break;
case PhoneListener.CALL_ERROR_GENERAL:
showPhoneEvent(errMsg + "General Error", callId);
break;
case PhoneListener.CALL_ERROR_HOLD_ERROR:
showPhoneEvent(errMsg + "Hold Error", callId);
break;
case PhoneListener.CALL_ERROR_INCOMING_CALL_BARRED:
showPhoneEvent(errMsg + "Incoming Call Barred", callId);
break;
case PhoneListener.CALL_ERROR_LOST_DUE_TO_FADING:
showPhoneEvent(errMsg + "Lost Due To Fading", callId);
break;
case PhoneListener.CALL_ERROR_MAINTENANCE_REQUIRED:
showPhoneEvent(errMsg + "Maintenance", callId);
break;
case PhoneListener.CALL_ERROR_NUMBER_NOT_IN_SERVICE:
showPhoneEvent(errMsg + "Number Not In Service", callId);
break;
case PhoneListener.CALL_ERROR_NUMBER_UNOBTAINABLE:
showPhoneEvent(errMsg + "Number Unobtainable", callId);
break;
case PhoneListener.CALL_ERROR_OUTGOING_CALLS_BARRED:
showPhoneEvent(errMsg + "Outgoing Calls Barred", callId);
break;
case PhoneListener.CALL_ERROR_PLEASE_TRY_LATER:
showPhoneEvent(errMsg + "Try Later", callId);
break;
case PhoneListener.CALL_ERROR_RADIO_PATH_UNAVAILABLE:
showPhoneEvent(errMsg + "Radio Path Unavailable", callId);
break;
case PhoneListener.CALL_ERROR_SERVICE_CONFLICT:
showPhoneEvent(errMsg + "Service Conflict", callId);
break;
case PhoneListener.CALL_ERROR_SERVICE_NOT_AVAILABLE:
showPhoneEvent(errMsg + "Service Not Available", callId);
break;
case PhoneListener.CALL_ERROR_SUBSCRIBER_BUSY:
showPhoneEvent(errMsg + "Subscriber Busy", callId);
break;
case PhoneListener.CALL_ERROR_SYSTEM_BUSY_TRY_LATER:
showPhoneEvent(errMsg + "System Busy", callId);
break;
case PhoneListener.CALL_ERROR_TRY_AGAIN:
showPhoneEvent(errMsg + "Try Again", callId);
break;
case PhoneListener.CALL_ERROR_USER_BUSY_IN_DATA:
showPhoneEvent(errMsg + "User Busy In Data", callId);
break;
case PhoneListener.CALL_ERROR_USER_BUSY_IN_PRIVATE:
showPhoneEvent(errMsg + "User Busy In Private", callId);
break;
case PhoneListener.CALL_ERROR_USER_NOT_AUTHORIZED:
showPhoneEvent(errMsg + "Not Authorized", callId);
break;
case PhoneListener.CALL_ERROR_USER_NOT_AVAILABLE:
showPhoneEvent(errMsg + "User Not Available", callId);
break;
case PhoneListener.CALL_ERROR_USER_NOT_REACHABLE:
showPhoneEvent(errMsg + "User Not Reachable", callId);
break;
}
}

Monday, February 2, 2009

How to - Capture power change events

0 comments

Your application can be notified when power change events take place on the BlackBerry device. This could be used to halt certain features when the battery power is low, and resume them when it is back to normal levels. Implementing the SystemListener interface (net.rim.device.api.system.SystemListener) can perform these operations. Events such as the BlackBerry powering on, powering off, and changes in the battery state can be captured. The following sample application shows how to implement the SystemListener interface:
Code:
/**
* PowerChangeEvent.java
*
*/

import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;

public class PowerChangeEvent
extends Application implements SystemListener
{
public static PowerChangeEvent theApp;

public static void main(String args[])
{
theApp = new PowerChangeEvent();
theApp.enterEventDispatcher();
}

public PowerChangeEvent()
{
//Register this instance with the system listener.
Application.getApplication().addSystemListener(this);
System.out.println("PowerChangeEvent: PowerChangeEvent has started!");
}

//Invoked when the user is putting the device into a power off state.
public void powerOff()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering off.");
}

//Invoked when the device has left the power off state.
public void powerUp()
{
System.out.println("PowerChangeEvent: The BlackBerry is powering up.");
}

//Invoked when the internal battery voltage falls below a critical level.
public void batteryLow()
{
System.out.println("PowerChangeEvent: The battery is getting low!");
}

//Invoked when the internal battery voltage has returned to normal.
public void batteryGood()
{
System.out.println("PowerChangeEvent: The battery level is now good!");
}

//Invoked when the internal battery state has changed.
//Not used in this sample.
public void batteryStatusChange(int status)
{
}
}