How to give an alert when the receiver accepts the call? - blackberry

I am developing one Phone Application. In that my requirement is when the receiver accepts the call then I give an alert(a small sound) at caller(who is calling) side that caller thinks that receiver accepts the call.
For this I am using the following code for an alert method when call is connected:
public void callConnected(int callId)
{
Alert.startAudio(GetTone.TUNE, 80);
try
{
System.out.println("====================Start Audio: "+System.currentTimeMillis());
Thread.sleep(2000);
Alert.stopAudio();
}
catch (Exception e)
{
e.printStackTrace();
}
}
But I am not getting any TUNE sound.
Am I did correct or not?
If not give some solution for this.

I get the sound from this small code:
public static short frequency = 1046;
public static short duration = 200;
public static int volume = 100;
and whenever I want the sound call this method:
public static void beepSound()
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Alert.startAudio(new short[]{frequency, duration}, volume);
}
});
}
Then I got the beep sound.

Related

how to pause and resume a download in javafx

I am building a download manager in javafx
I have added function to download button which initialises new task.More than one download is also being executed properly.
But I need to add pause and resume function. Please tell how to implement it using executor. Through execute function of Executors, task is being started but how do i pause & then resume it??
Below I am showing relevant portions of my code. Please tell if you need more details. thanks.
Main class
public class Controller implements Initializable {
public Button addDownloadButton;
public Button pauseResumeButton;
public TextField urlTextBox;
public TableView<DownloadEntry> downloadsTable;
ExecutorService executor;
#Override
public void initialize(URL location, ResourceBundle resources) {
// here tableview and table columns are initialised and cellValueFactory is set
executor = Executors.newFixedThreadPool(4);
}
public void addDownloadButtonClicked() {
DownloadEntry task = new DownloadEntry(new URL(urlTextBox.getText()));
downloadsTable.getItems().add(task);
executor.execute(task);
}
public void pauseResumeButtonClicked() {
//CODE FOR PAUSE AND RESUME
}
}
DownloadEntry.java
public class DownloadEntry extends Task<Void> {
public URL url;
public int downloaded;
final int MAX_BUFFER_SIZE=50*1024;
private String status;
//Constructor
public DownloadEntry(URL ur) throws Exception{
url = ur;
//other variables are initialised here
this.updateMessage("Downloading");
}
#Override
protected Void call() {
file = new RandomAccessFile(filename, "rw");
file.seek(downloaded);
stream = con.getInputStream();
while (status.equals("Downloading")) {
byte buffer=new byte[MAX_BUFFER_SIZE];
int c=stream.read(buffer);
if (c==-1){
break;
}
file.write(buffer,0,c);
downloaded += c;
status = "Downloading";
}
if (status.equals("Downloading")) {
status = "Complete";
updateMessage("Complete");
}
return null;
}
}
You may be interested in Concurrency in JavaFX.
I guess you should also have a look at pattern Observer.
By the way I think you should not use constant string as a status ("Downloading", etc), creating an enum would be a better approach.
In your loop, around the read/write part, there should be a synchronization mechanism, controlled by your pause/resume buttons (see the two links).

Tried to read incoming SMS content but getting Error in Blackberry

Hi friends i am trying to read incoming sms but getting warning like this . Invocation of questionable method: java.lang.String.(String) found in: mypackage.MyApp$ListeningThread.run()
Here is my code is
public class MyApp extends UiApplication {
//private ListeningThread listener;
public static void main(String[] args) {
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp() {
invokeAndWait(new Runnable() {
public void run() {
ListeningThread listener = new ListeningThread();
listener.start();
}
});
pushScreen(new MyScreen());
}
private static class ListeningThread extends Thread {
private boolean _stop = false;
private DatagramConnection _dc;
public synchronized void stop() {
_stop = true;
try {
_dc.close(); // Close the connection so the thread returns.
} catch (IOException e) {
System.err.println(e.toString());
}
}
public void run() {
try {
_dc = (DatagramConnection) Connector.open("sms://");
for (;;) {
if (_stop) {
return;
}
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
String address = new String(d.getAddress());
String msg = new String(d.getData());
if(msg.startsWith("START")){
Dialog.alert("hello");
}
System.out.println("Message received: " + msg);
System.out.println("From: " + address);
System.exit(0);
}
} catch (IOException e) {
System.err.println(e.toString());
}
}
}
}
Please correct me where i am wrong.Is possible give me some code to read incoming sms content in blackberry.
A few points about your code:
That invokeAndWait call to launch a thread makes no sense. It doesn't harm, but is kind of waste. Use that method only to perform UI related operations.
You should try using "sms://:0" as param for Connector.open. According to the docs, a parameter with the form {protocol}://[{host}]:[{port}] will open the connection in client mode (which makes sense, since you are on the receiving part), whereas not including the host part will open it in server mode.
Finally, if you can't get it working, you could use instead the third method specified in this tutorial, which you probably have already read.
The error you quoted is complaining about the use of the String constructor that takes a string argument. Since strings are immutable in Java-ME, this is just a waste. You can use the argument string directly:
Invocation of questionable method: java.lang.String.(String) found in: mypackage.MyApp$ListeningThread.run()
//String address = new String(d.getAddress());
String address = d.getAddress();
// getData() returns a byte[], so this is a different constructor
// However, this leaves the character encoding unspecified, so it
// will default to cp1252, which may not be what you want
String msg = new String(d.getData());

How to seek streaming media in blackberry

I'm using following code to forward streaming media. But it is giving me exception seeking not allowed on media.
Is there any way to seek streaming media in blackberry?
private void forwardPlayer(){
if(isPlaying){
try {
long prevTime = player.getMediaTime()/1000000;
final long newTime = prevTime + 5;
if(newTime <= player.getDuration()/1000000){
Thread t = new Thread(new Runnable() {
public void run() {
try {
player.setMediaTime(newTime * 1000000);
} catch (final MediaException e) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("forward "+e.toString());
}
});
}
}
});
t.start();
}
} catch (final Exception e) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("forward "+e.toString());
}
});
}
}
}
At the risk of stating the obvious, the reason why you cannot seek on your media is almost certainly because it is not possible to do so.
One solution would be to stream the data from your remote media source into a buffer which you can then seek on. Check out Streaming media - Start to finish for reference code.

How to silence an incoming call

I am trying to silence an incoming call and prevent the BlackBerry device from ringing. I tried Alert.setVolume(0) and some EventInjector keys but this didn't work.
So how to silence an incoming call?
I was puzzled by your question and decided to take up the challenge. I tried different thing including
Playing a "silence" audio file hoping to overlap the device's ringing or occupy the media player
Hacking the phone screen via UiApplication.getUiApplication().getActiveScreen()
Injecting keyboard events
Eventually, injecting VOLUME UP key (VOLUME DOWN key works as well) event worked for me and muted the device ringing on incoming call. The drawback with this approach is that sometimes the device did ring for a fraction of second before muting.
import net.rim.blackberry.api.phone.AbstractPhoneListener;
import net.rim.blackberry.api.phone.Phone;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.Keypad;
class Muter extends AbstractPhoneListener {
public void callIncoming(int callId) {
Thread muterThread = new Thread(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
muterThread.setPriority(Thread.MAX_PRIORITY);
muterThread.start();
}
}
public class MuterApp extends Application {
public static void main(String[] args){
Phone.addPhoneListener(new Muter());
new MyApp().enterEventDispatcher();
}
}
The following also works (replace Muter thread in callIncoming() method with the following code).
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char) Keypad.KEY_VOLUME_UP, 0));
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_UP, (char) Keypad.KEY_VOLUME_UP, 0));
}
});
You won't be able to disable the sound programmatically (found a couple other sources that said the same thing). The best workaround people have seemed to come up with was to use the EventInjector to change the phone's sound profile to silent.
Some Blackberry phones have a mute key. You may try the following idea:
public void callIncoming(int callId) {
if (KeyPad.hasMuteKey()) {
/* Inject KEY_SPEAKERPHONE event */
}
else {
/* Inject KEY_VOLUME_DOWN event N times, so that you get the mute effect */
}
}
i am quite new to all this...but i thought i might as well put in my 2 cents worth...
i have been trying to find ways to programatically change the profile settings...
i have found that, while we cannot(yet) change the profile settings, we can change the setting that we are using( change the profile thats in use, i think)- this is something i came across searching for info-though i should give credit to alishaik786 for the code.
public final class LoadingScreen extends MainScreen implements FieldChangeListener
{
public LoadingScreen()
{
createGUI();
}
private void createGUI()
{
try
{
ApplicationManager.getApplicationManager().launch("net_rim_bb_profiles_app");
}
catch (Exception e)
{
//Exception
}
}
public void fieldChanged(Field field, int context)
{
}
}

How to close streaming player screen when sp.realize() method executing

I have read the knowledgebase article "Streaming media - Start to finish" It is working fine. When I click the open video, the player screen is open. When I click the back button before the player is realized, it does not come to back to the right screen.
when sp.realize(); method executing user can't come to back screen.
after loading player. it close.
How to go back a screen if sp.realize() method is still executing?
new Thread(new Runnable()
{
public void run()
{
try
{
if(sp==null)
{
sp = new StreamingPlayer(url, contentType);
sp.setBufferCapacity(bufferCapacity);
sp.setInitialBuffer(initBuffer);
sp.setRestartThreshold(restartThreshold);
sp.setBufferLeakSize(bufferLeakSize);
sp.setConnectionTimeout(connectionTimeout);
sp.setLogLevel(logLevel);
sp.enableLogging(eventLogEnabled, sdLogEnabled);
sp.addStreamingPlayerListener(playerScreen);
sp.realize();
volC = (VolumeControl)sp.getControl("VolumeControl");
if(contentType.toLowerCase().indexOf("video")!=-1)
{
vidC = (VideoControl)sp.getControl("VideoControl");
videoField = (Field)vidC.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
vidC.setDisplaySize(Display.getWidth(), Display.getHeight()-timeSeeker.getHeight()-byteSeeker.getHeight());
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
replace(getField(0), videoField);
}
});
vidC.setVisible(true);
}
if(contentType.toLowerCase().indexOf("audio")!=-1)
{
audioIcon = true;
if(!(getField(0)==albumArt))
{
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run()
{
replace(videoField, (Field)albumArt);
}
});
}
}
sp.start();
}
else
{
sp.stop();
sp.close();
sp = null;
run();
}
} catch(Throwable t)
{
//log(t.toString());
}
}
}).start();
I'm not sure exactly what you mean by this. However, have you tried running the audio stuff in a separate thread? That should reduce the likelihood of it interfering with anything else.

Resources